本文共 6020 字,大约阅读时间需要 20 分钟。
log4j.properties
----------------------------------
## LOGGERS ##
#configure root loggerlog4j.rootLogger=DEBUG,console#DEBUG, INFO, WARN, ERROR, FATAL#define a logger named myLoggerlog4j.logger.myLogger=INFO,MAIL#define a second logger that is a child to myLoggerlog4j.logger.myLogger.mySonLogger=,file log4j.appender.console=org.apache.log4j.ConsoleAppenderlog4j.appender.console.layout=org.apache.log4j.PatternLayoutlog4j.appender.console.layout.ConversionPattern=[Test-log4j][%d{yyyy-MM-dd HH:mm:ss SSS}][%5p][%4r][%l]%m%n#org.apache.log4j.SimpleLayoutlog4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=./log/test-log4j.loglog4j.appender.file.DatePattern=.yyyyMMdd'.log'log4j.appender.file.layout=org.apache.log4j.PatternLayoutlog4j.appender.file.layout.ConversionPattern=[Test-log4j][%5p][%3r][%c][%t][%d{yyyy-MM-dd HH:mm:ss SSS}]%m%n ## [Test-log4j][%-5p][%3r][%c][%t][%d{yyyy-MM-dd HH:mm:ss SSS}]%m%n## %-5p表示占5个字符并左对齐,%5p表示右对齐## %l log输出调用的位置,如:cn.harry.logSystem.log4j.test.TestLog.main(TestLog.java:23)## %m 输出代码中指定的消息## %p 输出优先级,即DEBUG,INFO,WARN,ERROR,FATAL ## %r 输出自应用启动到输出该log信息耗费的毫秒数 ## %c 输出所属的类目,通常就是所在类的全名 ## %t 输出产生该日志事件的线程名 ## %n 输出一个回车换行符,Windows平台为“rn”,Unix平台为“n” ## %d 输出日志时间点的日期或时间,默认格式为ISO8601,也可以在其后指定格式,比如:%d{yyyy MMM dd HH:mm:ss,SSS},输出类似:2002年10月18日 22:10:28,921## %x ## %C## %F## %L## %M## %S## [%C][%F][%M][%L][%X][%l]%n 输出如下:## [cn.harry.logSystem.log4j.test.TestLog][TestLog.java][main][23][][cn.harry.logSystem.log4j.test.TestLog.main(TestLog.java:23)]## ## log4j.appender.console.layout=org.apache.log4j.HTMLLayout## log4j.appender.console.layout.LocationInfo=true## log4j.appender.console.layout.Title=this is the title of html page## # 应用于控制台
## log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender ## log4j.appender.Threshold=DEBUG ## log4j.appender.CONSOLE.Target=System.out ## log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout ## log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n ## #log4j.appender.CONSOLE.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD] n%c[CATEGORY]%n%m[MESSAGE]%n%n ## ## #应用于文件 ## log4j.appender.FILE=org.apache.log4j.FileAppender ## log4j.appender.FILE.File=file.log ## log4j.appender.FILE.Append=false ## log4j.appender.FILE.layout=org.apache.log4j.PatternLayout ## log4j.appender.FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n ## # Use this layout for LogFactor 5 analysis ## ## # 应用于文件回滚 ## log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender ## log4j.appender.ROLLING_FILE.Threshold=ERROR ## log4j.appender.ROLLING_FILE.File=rolling.log //文件位置,也可以用变量${java.home}、rolling.log## log4j.appender.ROLLING_FILE.Append=true //true:添加 false:覆盖## log4j.appender.ROLLING_FILE.MaxFileSize=10KB //文件最大尺寸## log4j.appender.ROLLING_FILE.MaxBackupIndex=1 //备份数## log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout ## log4j.appender.ROLLING_FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n ## ## #应用于socket ## log4j.appender.SOCKET=org.apache.log4j.RollingFileAppender ## log4j.appender.SOCKET.RemoteHost=localhost ## log4j.appender.SOCKET.Port=5001 ## log4j.appender.SOCKET.LocationInfo=true ## # Set up for Log Facter 5 ## log4j.appender.SOCKET.layout=org.apache.log4j.PatternLayout ## log4j.appender.SOCET.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD]%n%c[CATEGORY]%n%m[MESSAGE]%n%n ## ## # Log Factor 5 Appender ## log4j.appender.LF5_APPENDER=org.apache.log4j.lf5.LF5Appender ## log4j.appender.LF5_APPENDER.MaxNumberOfRecords=2000 ##### # 发送日志给邮件 log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender log4j.appender.MAIL.Threshold=FATAL log4j.appender.MAIL.EvaluatorClass=cn.harry.logSystem.log4j.test.MailTriggeringEventEvaluatorlog4j.appender.MAIL.BufferSize=10log4j.appender.MAIL.SMTPHost=192.168.1.12
log4j.appender.MAIL.Subject=log4j-test maillog4j.appender.MAIL.layout=org.apache.log4j.PatternLayout log4j.appender.MAIL.layout.ConversionPattern=[Test-log4j][%5p][%3r][%c][%t][%d{yyyy-MM-dd HH:mm:ss SSS}]%m%n## [framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n## # 用于数据库
## log4j.appender.DATABASE=org.apache.log4j.jdbc.JDBCAppender ## log4j.appender.DATABASE.URL=jdbc:mysql://localhost:3306/test ## log4j.appender.DATABASE.driver=com.mysql.jdbc.Driver ## log4j.appender.DATABASE.user=root ## log4j.appender.DATABASE.password= ## log4j.appender.DATABASE.sql=INSERT INTO LOG4J (Message) VALUES ('[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n') ## log4j.appender.DATABASE.layout=org.apache.log4j.PatternLayout ## log4j.appender.DATABASE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n ## ## log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender ## log4j.appender.A1.File=SampleMessages.log4j ## log4j.appender.A1.DatePattern=yyyyMMdd-HH'.log4j' ## log4j.appender.A1.layout=org.apache.log4j.xml.XMLLayout## #自定义Appender
## log4j.appender.im = net.cybercorlin.util.logger.appender.IMAppender ## log4j.appender.im.host = mail.cybercorlin.net ## log4j.appender.im.username = username ## log4j.appender.im.password = password ## log4j.appender.im.recipient =## log4j.appender.im.layout=org.apache.log4j.PatternLayout
## log4j.appender.im.layout.ConversionPattern =[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n 按照package的方式控制:--------------------------------------------------------------------------------------------------# Control logging for other open source packages
log4j.logger.org.apache.commons=ERRORlog4j.logger.org.apache.struts=WARNlog4j.logger.org.displaytag=ERRORlog4j.logger.org.springframework=infolog4j.logger.com.ibatis.db=WARNlog4j.logger.org.apache.cactus=WARNlog4j.logger.org.apache.commons.httpclient=ERROR需要与如下语句配套使用:
Logger myLogger = Logger.getLogger(this.getClass());或:Logger myLogger = Logger.getLogger("org.apache.commons.httpclient.aaa");子包继承于父包的logger
参考:
书籍:《log4j The Complete Manual[en].pdf》
转载地址:http://zwvad.baihongyu.com/