Log4j: 較完整的設定參考

資料來源 – http://hi.baidu.com/ibelieve9/blog/item/6c16b8aff139c4c87dd92a6d.html

log4j.rootLogger=INFO,consoleAppender,logfile,MAIL
log4j.addivity.org.apache=true

#ConsoleAppender,控制台输出
#FileAppender,文件日志输出
#SMTPAppender,发邮件输出日志
#SocketAppender,Socket日志
#NTEventLogAppender,Window NT日志
#SyslogAppender,
#JMSAppender,
#AsyncAppender,
#NullAppender

#文件输出:RollingFileAppender
#log4j.rootLogger = INFO,logfile
log4j.appender.logfile = org.apache.log4j.RollingFileAppender
log4j.appender.logfile.Threshold = INFO  
# 输出以上的INFO信息
log4j.appender.logfile.File = INFO_log.html    
#保存log文件路径
log4j.appender.logfile.Append = true   
# 默认为true,添加到末尾,false在每次启动时进行覆盖
log4j.appender.logfile.MaxFileSize = 1MB
# 一个log文件的大小,超过这个大小就又会生成1个日志 # KB ,MB,GB
log4j.appender.logfile.MaxBackupIndex = 3  
# 最多保存3个文件备份
log4j.appender.logfile.layout = org.apache.log4j.HTMLLayout
# 输出文件的格式
log4j.appender.logfile.layout.LocationInfo = true
#是否显示类名和行数
log4j.appender.logfile.layout.Title =title:\u63d0\u9192\u60a8\uff1a\u7cfb\u7edf\u53d1\u751f\u4e86\u4e25\u91cd\u9519\u8bef
#html页面的 < title >
############################## SampleLayout ####################################
# log4j.appender.logfile.layout = org.apache.log4j.SampleLayout
############################## PatternLayout ###################################
# log4j.appender.logfile.layout = org.apache.log4j.PatternLayout
# log4j.appender.logfile.layout.ConversionPattern =% d % p [ % c] -   % m % n % d
############################## XMLLayout #######################################
# log4j.appender.logfile.layout = org.apache.log4j.XMLLayout
# log4j.appender.logfile.layout.LocationInfo = true #是否显示类名和行数
############################## TTCCLayout ######################################
# log4j.appender.logfile.layout = org.apache.log4j.TTCCLayout
# log4j.appender.logfile.layout.DateFormat = ISO8601
#NULL, RELATIVE, ABSOLUTE, DATE or ISO8601.
# log4j.appender.logfile.layout.TimeZoneID = GMT - 8 : 00
# log4j.appender.logfile.layout.CategoryPrefixing = false ##默认为true 打印类别名
# log4j.appender.logfile.layout.ContextPrinting = false ##默认为true 打印上下文信息
# log4j.appender.logfile.layout.ThreadPrinting = false ##默认为true 打印线程名
# 打印信息如下:
#2007 - 09 - 13   14 : 45 : 39 , 765 [http - 8080 - 1 ] ERROR com.poxool.test.test - error成功关闭链接
###############################################################################
#每天文件的输出:DailyRollingFileAppender
#log4j.rootLogger = INFO,errorlogfile
log4j.appender.errorlogfile = org.apache.log4j.DailyRollingFileAppender
log4j.appender.errorlogfile.Threshold = ERROR
log4j.appender.errorlogfile.File = ../logs/ERROR_log
log4j.appender.errorlogfile.Append = true
#默认为true,添加到末尾,false在每次启动时进行覆盖
log4j.appender.errorlogfile.ImmediateFlush = true  
#直接输出,不进行缓存
# ' . ' yyyy - MM: 每个月更新一个log日志
# ' . ' yyyy - ww: 每个星期更新一个log日志
# ' . ' yyyy - MM - dd: 每天更新一个log日志
# ' . ' yyyy - MM - dd - a: 每天的午夜和正午更新一个log日志
# ' . ' yyyy - MM - dd - HH: 每小时更新一个log日志
# ' . ' yyyy - MM - dd - HH - mm: 每分钟更新一个log日志
log4j.appender.errorlogfile.DatePattern = ' . ' yyyy - MM - dd ' .log '
#文件名称的格式
log4j.appender.errorlogfile.layout = org.apache.log4j.PatternLayout
log4j.appender.errorlogfile.layout.ConversionPattern =%d %p [ %c] -   %m %n %d

#控制台输出:
#log4j.rootLogger = INFO,consoleAppender
log4j.appender.consoleAppender = org.apache.log4j.ConsoleAppender
log4j.appender.consoleAppender.Threshold = ERROR
log4j.appender.consoleAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.consoleAppender.layout.ConversionPattern =%d %-5p %m %n
log4j.appender.consoleAppender.ImmediateFlush = true

# 直接输出,不进行缓存
log4j.appender.consoleAppender.Target = System.err
# 默认是System.out方式输出

#发送邮件:SMTPAppender
#log4j.rootLogger = INFO,MAIL
log4j.appender.MAIL = org.apache.log4j.net.SMTPAppender
log4j.appender.MAIL.Threshold = INFO
log4j.appender.MAIL.BufferSize = 10
log4j.appender.MAIL.From = yourmail@gmail.com
log4j.appender.MAIL.SMTPHost = smtp.gmail.com
log4j.appender.MAIL.Subject = Log4J Message
log4j.appender.MAIL.To = yourmail@gmail.com
log4j.appender.MAIL.layout = org.apache.log4j.PatternLayout
log4j.appender.MAIL.layout.ConversionPattern =%d - %c -%-4r [%t] %-5p %c %x - %m %n

#数据库:JDBCAppender
log4j.appender.DATABASE = org.apache.log4j.jdbc.JDBCAppender
log4j.appender.DATABASE.URL = jdbc:oracle:thin:@ 210.51 . 173.94 : 1521 :YDB
log4j.appender.DATABASE.driver = oracle.jdbc.driver.OracleDriver
log4j.appender.DATABASE.user = ydbuser
log4j.appender.DATABASE.password = ydbuser
log4j.appender.DATABASE.sql = INSERT INTO A1 (TITLE3) VALUES ( ' %d - %c %-5p %c %x - %m%n ' )
log4j.appender.DATABASE.layout = org.apache.log4j.PatternLayout
log4j.appender.DATABASE.layout.ConversionPattern =% d -   % c -%- 4r [ % t] %- 5p % c % x -   % m % n
#数据库的链接会有问题,可以重写org.apache.log4j.jdbc.JDBCAppender的getConnection() 使用数据库链接池去得链接,可以避免insert一条就链接一次数据库。

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s