java 日志技術匯總(log4j , Commons-logging,.....)
前言
在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 { @Override public 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,logfile log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n log4j.appender.logfile=org.apache.log4j.RollingFileAppender log4j.appender.logfile.File=logfile.log log4j.appender.logfile.MaxFileSize=512KB log4j.appender.logfile.MaxBackupIndex=3 log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.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 log Logger log = Logger.getLogger(TestLog4j.class); // 2. get log config file PropertyConfigurator.configure("log4j.properties"); // 3. start log 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"); } }
配置稍顯麻煩,但是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獲取日志處理類時:
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 之外, 還可以配置
總結
以上有一條
3) 否則,查看classpath中是否有Log4j的包,如果發現,則自動使用Log4j作為日志實現類;
項目同時導入log4j 和commons-logging的jar 包, 不需要配置commons-logging.properties ,只需要在classpath中配置 log4j.properties就可以使用log4j的方式記錄日志。這也是目前用的比較多的記錄日志的方式。
來自:http://blog.csdn.net/oscar999/article/details/9698489