Log4j是Apache的一個開放源代碼項目,通過使用Log4j,我們可以控制日誌信息輸送的目的地是控制檯、文件、GUI組件,甚至是套接口服務器、NT的事件記錄器、UNIX Syslog守護進程等;我們也可以控制每一條日誌的輸出格式;通過定義每一條日誌信息的級別,我們能夠更加細緻地控制日誌的生成過程。最令人感興趣的就是,這些可以通過一個配置文件來靈活地進行配置,而不需要修改應用的代碼。
【1】引入jar包
官網jar包下載地址:http://logging.apache.org/log4j/1.2/download.html
附件內的工程我也捎帶了log4j-1.2.15.jar包
【2】配置文件log4j.properties
# Log4J Settings for log4j 1.2.x (via jakarta-commons-logging)
#
# The five logging levels used by Log are (in order):
#
# 1. DEBUG (the least serious)
# 2. INFO
# 3. WARN
# 4. ERROR
# 5. FATAL (the most serious)
# Set root logger level to INFO and append to stdout
log4j.rootLogger=INFO,stdout,file
#設置log的級別爲INFO意味着INFO級別以下如DEBUG的信息不會輸出
#其中,Log4j提供的appender有以下幾種:
#org.apache.log4j.ConsoleAppender(控制檯),
#org.apache.log4j.FileAppender(文件),
#org.apache.log4j.DailyRollingFileAppender(每天產生一個日誌文件),
#org.apache.log4j.RollingFileAppender(文件大小到達指定尺寸的時候產生一個新的文件),
#org.apache.log4j.WriterAppender(將日誌信息以流格式發送到任意指定的地方)
#其中,Log4j提供的layout有以幾種:
#org.apache.log4j.HTMLLayout(以HTML表格形式佈局),
#org.apache.log4j.PatternLayout(可以靈活地指定佈局模式),
#org.apache.log4j.SimpleLayout(包含日誌信息的級別和信息字符串),
#org.apache.log4j.TTCCLayout(包含日誌產生的時間、線程、類別等等信息)
#Log4J採用類似C語言中的printf函數的打印格式格式化日誌信息,打印參數如下:
# %m 輸出代碼中指定的消息
# %p 輸出優先級,即DEBUG,INFO,WARN,ERROR,FATAL
# %r 輸出自應用啓動到輸出該log信息耗費的毫秒數
# %c 輸出所屬的類目,通常就是所在類的全名
# %t 輸出產生該日誌事件的線程名
# %n 輸出一個回車換行符,Windows平臺爲“rn”,Unix平臺爲“n”
# %d 輸出日誌時間點的日期或時間,默認格式爲ISO8601,也可以在其後指定格式,比如:%d{yyy MMM dd HH:mm:ss,SSS},輸出類似:2002年10月18日 22:10:28,921
# %l 輸出日誌事件的發生位置,包括類目名、發生的線程,以及在代碼中的行數。舉例:Testlog4.main(TestLog4.java:10)
#stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%p]%-d{yyyy-MM-dd HH\:mm\:ss}[%c\:%L] %m%n
#file
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=/output.log
log4j.appender.file.DatePattern='.'yyyy-MM-dd
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%p]%-d{yyyy-MM-dd HH\:mm\:ss}[%c\:%L] %m%n
#log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
#log4j.appender.file.File=/output.html
#log4j.appender.file.DatePattern='.'yyyy-MM-dd
#log4j.appender.file.layout=org.apache.log4j.HTMLLayout
#log4j.appender.file.layout.ConversionPattern=[%p]%-d{yyyy-MM-dd HH\:mm\:ss}[%c\:%L] %m%n
# Print only messages of level ERROR or above in the package noModule.
log4j.logger.noModule=DEBUG
【3】測試文件
private static final Log log = LogFactory.getLog(ProcessMain.class);
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
long processTime = System.currentTimeMillis();
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(processTime)).toString();
log.debug("debug: "+time);
log.info("info: "+time);
log.error("error: "+time);
}
【4】結果文件輸出
[INFO]2014-12-31 20:48:58[hhf.log.ProcessMain:21] info: 2014-12-31 20:48:58
[ERROR]2014-12-31 20:48:58[hhf.log.ProcessMain:22] error: 2014-12-31 20:48:58
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Log4J Log Messages</title>
<style type="text/css">
<!--
body, table {font-family: arial,sans-serif; font-size: x-small;}
th {background: #336699; color: #FFFFFF; text-align: left;}
-->
</style>
</head>
<body bgcolor="#FFFFFF" topmargin="6" leftmargin="6">
<hr size="1" noshade>
Log session start time Wed Dec 31 20:50:27 CST 2014<br>
<br>
<table cellspacing="0" cellpadding="4" border="1" bordercolor="#224466" width="100%">
<tr>
<th>Time</th>
<th>Thread</th>
<th>Level</th>
<th>Category</th>
<th>Message</th>
</tr>
<tr>
<td>0</td>
<td title="main thread">main</td>
<td title="Level"><font color="#339933">DEBUG</font></td>
<td title="hhf.log.ProcessMain category">hhf.log.ProcessMain</td>
<td title="Message">debug: 2014-12-31 20:50:27</td>
</tr>
<tr>
<td>3</td>
<td title="main thread">main</td>
<td title="Level">INFO</td>
<td title="hhf.log.ProcessMain category">hhf.log.ProcessMain</td>
<td title="Message">info: 2014-12-31 20:50:27</td>
</tr>
<tr>
<td>4</td>
<td title="main thread">main</td>
<td title="Level"><font color="#993300"><strong>ERROR</strong></font></td>
<td title="hhf.log.ProcessMain category">hhf.log.ProcessMain</td>
<td title="Message">error: 2014-12-31 20:50:27</td>
</tr>
(PS:附上測試工程)