1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
/** * */ import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; /** * @author magic282 * */ public class Logger { private static String logFilePath; private static boolean isInitialized = false ; private static FileWriter logWriter = null ; private static boolean printLogWhenLog = true ; private static boolean InitLogger() { String logDirectoryPath = System.getProperty( "user.dir" ) + java.io.File.separatorChar + "log" ; if (! new File(logDirectoryPath).exists()) { new File(logDirectoryPath).mkdir(); } Date logfileDate = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd-HH-mm-ss" ); logFilePath = logDirectoryPath + java.io.File.separatorChar + dateFormat.format(logfileDate) + ".log" ; try { logWriter = new FileWriter(logFilePath, true ); isInitialized = true ; } catch (IOException e) { // TODO Auto-generated catch block System.err.println( "Unable to create log file." ); System.err.println( "Initilization fail." ); e.printStackTrace(); return false ; } return true ; } public static void Log(String message) { if (!isInitialized) { InitLogger(); } Date logfileDate = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd-HH-mm-ss" ); String callingClassName = new Exception().getStackTrace()[ 1 ] .getClassName(); synchronized (logWriter) { String log = String.format( "[%s] @ [%s]: %s\n" , callingClassName, dateFormat.format(logfileDate), message); if (printLogWhenLog) { System.out.printf( "[log]:%s" , log); } try { logWriter.write(log); logWriter.flush(); } catch (IOException e) { // TODO Auto-generated catch block System.err.println( "Write log to file %s error." ); e.printStackTrace(); } } } public static void Log(Exception exception) { if (!isInitialized) { InitLogger(); } Date logfileDate = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd-HH-mm-ss" ); String callingClassName = new Exception().getStackTrace()[ 1 ] .getClassName(); synchronized (logWriter) { String log = String.format( "[%s] @ [%s]: %s\n" , callingClassName, dateFormat.format(logfileDate), exception.toString()); if (printLogWhenLog) { System.out.printf( "[log]:%s" , log); } try { logWriter.write(log); logWriter.flush(); } catch (IOException e) { // TODO Auto-generated catch block System.err.println( "Write log to file %s error." ); e.printStackTrace(); } } } } |
自己寫(xiě)的簡(jiǎn)易版Java日志類(lèi)分享
2019-12-23 15:21junjie JAVA教程
這篇文章主要介紹了自己寫(xiě)的簡(jiǎn)易版Java日志類(lèi)分享,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下
延伸 · 閱讀
- 2019-12-23java使用POI讀取properties文件并寫(xiě)到Excel的方法
- 2019-12-23java中struts配置
- 2019-12-23淺談Java轉(zhuǎn)義符\\|
- 2019-12-23Java中的Object類(lèi)詳細(xì)介紹
- 2019-12-23Java中一個(gè)for語(yǔ)句導(dǎo)致無(wú)窮大死循環(huán)的例子
- 2019-12-23Java中遍歷數(shù)組使用foreach循環(huán)還是for循環(huán)?
- JAVA教程
Java8的default方法詳細(xì)介紹
這篇文章主要介紹了Java8的default方法,詳細(xì)介紹了什么是default方法,在多繼承時(shí)的處理等,需要的朋友可以參考下 ...
- JAVA教程
Java中final變量使用總結(jié)
這篇文章主要介紹了Java中final變量使用總結(jié),final關(guān)鍵字可用于變量聲明,一旦該變量被設(shè)定,就不可以再改變?cè)撟兞康闹?通常final定義的變量為常量,需要的朋...
- JAVA教程
java遍歷讀取xml文件內(nèi)容
這篇文章主要為大家介紹了java遍歷讀取xml文件內(nèi)容,感興趣的小伙伴們可以參考一下...
- JAVA教程
Java并發(fā)編程中構(gòu)建自定義同步工具
這篇文章主要介紹了Java并發(fā)編程中構(gòu)建自定義同步工具,本文講解了可阻塞狀態(tài)依賴(lài)操作的結(jié)構(gòu)、有界緩存實(shí)現(xiàn)基類(lèi)示例、阻塞實(shí)現(xiàn)方式一:拋異常給調(diào)用...
- JAVA教程
分享幾個(gè)提高Java性能的高效用法
這篇文章主要介紹了分享幾個(gè)提高Java性能的高效用法 ,需要的朋友可以參考下 ...
- JAVA教程
java多線(xiàn)程編程之從線(xiàn)程返回?cái)?shù)據(jù)的兩種方法
從線(xiàn)程中返回?cái)?shù)據(jù)和向線(xiàn)程傳遞數(shù)據(jù)類(lèi)似。也可以通過(guò)類(lèi)成員以及回調(diào)函數(shù)來(lái)返回?cái)?shù)據(jù)。但類(lèi)成員在返回?cái)?shù)據(jù)和傳遞數(shù)據(jù)時(shí)有一些區(qū)別,下面讓我們來(lái)看看...
- JAVA教程
JAVA中字符串函數(shù)subString的用法小結(jié)
本篇文章主要是對(duì)JAVA中字符串函數(shù)subString的用法進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助 ...
- JAVA教程
java配置dbcp連接池(數(shù)據(jù)庫(kù)連接池)示例分享
java配置dbcp連接池示例分享,大家參考使用吧 ...