前言
在java 中實現記錄日誌的方式有很多種,
1. 最簡單的方式,就是system.print.out ,err 這樣直接在控制台列印訊息了。
2. java.util.logging ; 在JDK 1.4 版本之後,提供了日誌的API ,可以往檔案中寫日誌了。
3. log4j , 最強大的記錄日誌的方式。 可以通過配置 .properties 或是 .xml 的檔案, 配置日誌的目的地,格式等等。
4. commons-logging, 最綜合和常見的日誌記錄方式, 經常是和log4j 結合起來使用。
java.util.logging --JDK 記錄日誌方式
system.print 這就不用多說了,
直接看一下java api 中 logging 日誌的使用例子:
/** * @author oscar999 * @date 2013-8-1 * @version V1.0 */package com.oscar999.log;import java.io.IOException;import java.util.Date;import java.util.logging.FileHandler;import java.util.logging.Formatter;import java.util.logging.Level;import java.util.logging.LogRecord;import java.util.logging.Logger;public class TestLogJava {public static void main(String[] args) throws IOException{Logger log = Logger.getLogger("tesglog");log.setLevel(Level.ALL);FileHandler fileHandler = new FileHandler("testlog.log");fileHandler.setLevel(Level.ALL);fileHandler.setFormatter(new LogFormatter());log.addHandler(fileHandler);log.info("This is test java util log"); }}class LogFormatter extends Formatter {@Overridepublic String format(LogRecord record) {Date date = new Date();String sDate = date.toString();return "[" + sDate + "]" + "[" + record.getLevel() + "]"+ record.getClass() + record.getMessage() + "\n";}}
這裡是在eclipse 下code 和測試的。
首先定義一個Logeer的執行個體,並設定log 的層級,接著添加一個fileHander ,就是把日誌寫到檔案中。在寫入檔案的時候,定義一個 LogFormatter對日誌進行格式的渲染。
預設狀況下, 日誌會列印到控制台。添加filehandler 後, 會同時寫入檔案。 如不指定路徑,記錄檔將位於項目根路徑下。
log4j 記錄日誌方式
log4j 是apache 提供的記錄日誌的jar 檔。
下載路徑:
http://logging.apache.org/log4j/1.2/download.html
這裡要做的事情稍微要多一些:
1. 下載log4j 的jar 包,放入項目的lib 包中(添加到項目的build path中)。
2. 配置log4j.properties, 並放入項目的根路徑下.(也可以放入其他路徑,在讀的時候需要指定)
看一下一個配置執行個體:
log4j.rootLogger=debug,stdout,logfilelog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%nlog4j.appender.logfile=org.apache.log4j.RollingFileAppenderlog4j.appender.logfile.File=logfile.loglog4j.appender.logfile.MaxFileSize=512KBlog4j.appender.logfile.MaxBackupIndex=3log4j.appender.logfile.layout=org.apache.log4j.PatternLayoutlog4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
這裡指定了日誌輸出的層級 debug.
stdout, logfile 指定日誌輸出的目的地。 這兩個名字可以隨便取,比如 A, 或B都可以。 實際的配置是 org.apache.log4j.ConsoleAppender 和RollingFileAppender 用於指定是控制台還是檔案。
另外還指定了輸出的格式, 已經產生的file 的規則。
3. 測試java 檔案
/** * @author oscar999 * @date 2013-8-1 * @version V1.0 */package com.oscar999.log;import org.apache.log4j.Logger;import org.apache.log4j.PropertyConfigurator;public class TestLog4j {public static void main(String[] args) {// 1. create logLogger log = Logger.getLogger(TestLog4j.class);// 2. get log config filePropertyConfigurator.configure("log4j.properties");// 3. start loglog.debug("Here is some DEBUG");log.info("Here is some INFO");log.warn("Here is some WARN");log.error("Here is some ERROR");log.fatal("Here is some FATAL");}}
配置稍顯麻煩,但是code 時就簡單多了。
commons-logging寫日誌方式
Commons-logging 也是Apache 提供的日誌jar 檔。
:
http://commons.apache.org/proper/commons-logging/download_logging.cgi
你有可能要問為什麼有了log4j還有提供Commons-logging呢? 這兩者有什麼區別嗎?
其實從Commons-logging這個名字就可以看出來, 這應該是一個日誌的共用介面。實際上, 它的確是這樣一個作用,
使用Commons-logging的LogFactory擷取Tlog類時:
1) 首先在classpath下尋找自己的設定檔commons-logging.properties,如果找到,則使用其中定義的Log實作類別;
2) 如果找不到commons-logging.properties檔案,則在尋找是否已定義系統內容變數org.apache.commons.logging.Log,找到則使用其定義的Log實作類別;
如果在Tomact中可以建立一個叫 :CATALINA_OPTS 的環境變數
給 他的 值 : - Dorg.apache.commons.logging.Log = org.apache.commons.logging.impl.SimpleLog - Dorg.apache.commons.logging.simplelog.defaultlog = warn
3) 否則,查看classpath中是否有Log4j的包,如果發現,則自動使用Log4j作為日誌實作類別;
4) 否則,使用JDK自身的日誌實作類別(JDK1.4以後才有日誌實作類別);
5) 否則,使用commons-logging自己提供的一個簡單的日誌實作類別SimpleLog;
先使用第一種方式來看一個執行個體,配置commons-logging.properties, 使用log4j來記錄日誌。
注意, commons-logging 要配合log4j 記錄日誌,必須把log4j的jar 包也匯入到項目中。
1. 匯入log4j 和commons-logging的jar 包
2. 配置commons-logging.properties 和 log4j.properties, 放入項目的classpath下(也就是src目錄下)
注意: 單獨使用log4j 的時候,log4j.properties 預設是放在項目的根目錄下。
log4j.properties 的內容和上面完全相同。
看一下commons-logging.properties 的配置
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
一句話,指定使用log4j
3. 測試代碼:
/** * @author oscar999 * @date 2013-8-1* @version V1.0 */package com.oscar999.log;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;public class TestLogCom {static Log log = LogFactory.getLog(TestLog.class);public static void main(String[] args) {log.debug("Here is some DEBUG");log.info("Here is some INFO");log.warn("Here is some WARN");log.error("Here is some ERROR");log.fatal("Here is some FATAL");}}
除了使用log4j 之外, 還可以配置
-org.apache.commons.logging.impl.
Jdk14Logger 使用JDK1.4。 -org.apache.commons.logging.impl.
Log4JLogger 使用Log4J。 -org.apache.commons.logging.impl.
LogKitLogger 使用 avalon-Logkit。 -org.apache.commons.logging.impl.
SimpleLog common-logging內建日誌實作類別。它實現了Log介面,把日誌訊息都輸出到系統錯誤流System.err 中。 -org.apache.commons.logging.impl.
NoOpLog common-logging內建日誌實作類別。它實現了Log介面。 其輸出日誌的方法中不進行任何操作。
總結
以上有一條
3) 否則,查看classpath中是否有Log4j的包,如果發現,則自動使用Log4j作為日誌實作類別;
項目同時匯入log4j 和commons-logging的jar 包, 不需要配置commons-logging.properties ,只需要在classpath中配置 log4j.properties就可以使用log4j的方式記錄日誌。這也是目前用的比較多的記錄日誌的方式。