Log4J日志文件配置
------------------------------------------------------------------------
一、日志器配置
# 根日志器(其他日志器都继承自这个日志器)
# Default Level=DEBUG
log4j.rootLogger=INFO, A1, A2 (A1,A2为Appender)
# 日志的层次
log4j.logger.A=INFO,A1
log4j.logger.A.B=DEBUG,A1,A2
则日志器的继承关系为:A.B继承自A,A继承自root logger,即发往A.B的INFO日志,也会被A记录,也会被root logger记录
注意:A.B不一定是包名加类名,你可以任意取名
代码:
Logger logger = Logger.getLogger("A.B");
logger.debug("Test Log");
则A.B日志器会继承A和root logger的appender,因此"Test Log"会输出三次,其中A日志的层次INFO不影响,仍然会输出"Test Log"
HttpClient的Log4J配置
# commons.httpclient
log4j.logger.httpclient.wire.header=INFO
log4j.logger.httpclient.wire=INFO
log4j.logger.org.apache.commons.httpclient=INFO
二、Appender配置
# Console Appender=A1,File Appender=A2,DB Appender=A3,Mail Appender=A4
# A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.Threshold=DEBUG
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-5p Logger:%c{1} Date:%d{ISO8601} - %m%n
# A2
log4j.appender.A2=org.apache.log4j.RollingFileAppender
log4j.appender.A2.Threshold=DEBUG
# ${logHome}和${logName}从系统环境变量中读取[System.setProperty(prop,value)]
log4j.appender.A2.File=${logHome}/${logName}
log4j.appender.A2.Append=true
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%-5p Logger:%c{1} Date:%d{ISO8601} - %m%n
log4j.appender.A2.MaxBackupIndex=10
log4j.appender.A2.MaxFileSize=2MB
# A3
log4j.appender.A3=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.A3.Threshold=WARN
log4j.appender.A3.URL=jdbc:mysql://localhost:3306/test
log4j.appender.A3.driver=com.mysql.jdbc.Driver
log4j.appender.A3.user=root
log4j.appender.A3.password=
log4j.appender.A3.sql=INSERT INTO LOG4J(Message) VALUES ('%-5p Logger:%c{1} Date:%d{ISO8601} - %m%n')
log4j.appender.A3.layout=org.apache.log4j.PatternLayout
log4j.appender.A3.layout.ConversionPattern=%-5p Logger:%c{1} Date:%d{ISO8601} - %m%n
# A4
log4j.appender.A4=org.apache.log4j.net.SMTPAppender
log4j.appender.A4.Threshold=ERROR
log4j.appender.A4.BufferSize=10
log4j.appender.A4.SMTPUsername=qydd_program
log4j.appender.A4.SMTPPassword=
log4j.appender.A4.SMTPHost=smtp.mail.yahoo.com.cn
log4j.appender.A4.SMTPDebug=true
log4j.appender.A4.From=qydd_program@yahoo.com.cn
log4j.appender.A4.Subject=BaseFrame Error Message
log4j.appender.A4.To=qydd_program@yahoo.com.cn
log4j.appender.A4.layout=org.apache.log4j.PatternLayout
log4j.appender.A4.layout.ConversionPattern=%-5p Logger:%c{1} Date:%d{ISO8601} - %m%n
三、代码示例
输出:
WARN Logger:A Date:2009-10-02 01:13:52,734 - Test2
WARN Logger:A Date:2009-10-02 01:13:52,734 - Test2
INFO Logger:B Date:2009-10-02 01:13:52,734 - Test3
INFO Logger:B Date:2009-10-02 01:13:52,734 - Test3
INFO Logger:B Date:2009-10-02 01:13:52,734 - Test3
分析:
(1)因为root logger为warn,其输出为debug,因此"Test1"不打印
(2)A Logger继承自root logger,其层次为warn,输出为warn,所以输出二个"Test2"
(3)B Logger继承自A Logger,A Logger继承自root logger,其层次为info,所以输出三个"Test3",而“Test4"不输出
(4)B Logger打印的是info("Test3")满足A.B的层次,但不满足A和root logger的层次warn,但是log4j只是将A和root logger的appender附加到A.B上,因此level对B Logger的打印不影响
(5)在log4j.properties中加入log4j.additivity.A.B=false一行,使得A.B日志器的输出只在其本身输出,不在A和root logger输出