log4j.rootLogger=INFO,DATABASE
# ************************************************* #
# log put into database
# ************************************************* #
log4j.appender.DATABASE=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.DATABASE.URL=jdbc:mysql://localhost:3306/lsoba
log4j.appender.DATABASE.driver=com.mysql.jdbc.Driver
log4j.appender.DATABASE.user=root
log4j.appender.DATABASE.password=lijian
log4j.appender.DATABASE.sql=INSERT INTO loginfo (date,thread,level,logclass,message,user) values ('%d{yyyy-MM-dd HH:mm:ss}', '%t', '%p', '%l', '%m','%X{user}')
log4j.appender.DATABASE.layout=org.apache.log4j.PatternLayout
但是要按找自己所想的随心所遇的记录,那还是要费一番功夫的。
^_^,经过我一下午的折腾。终于彻底玩转log4j
首先准备log4j.properties文件,内容如下,至于每句话都代表什么意思,我就不多解释了。GOOGLE一下,满山遍野的都是
结果:
1.日志路径记录到当前应用的WEB-INF/logs目录下,分10个大小为1M的文件记录JFrame.log,JFrame.log.0...
2.控制台的日志,可以直接点击导航到源码位置!
log4j.rootLogger=DEBUG, CONSOLE,ROLLING_FILE
log4j.addivity.org.apache=true
# 应用于控制台
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=------------------ %d{yyyy-MM-dd HH:mm:ss} ------------------%nlevel: %p%nrooter: %C.%M(%c{1}.java:%L) %nmessage: %m%n
#log4j.appender.CONSOLE.layout.ConversionPattern=[JFrame] %d at %c.%M(%L) %-5p %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=${webapp.root}/WEB-INF/logs/JFrame.log
#log4j.appender.FILE.Append=false
#log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
#log4j.appender.FILE.layout.ConversionPattern=------------------ %d{yyyy-MM-dd HH:mm:ss} ------------------%nlevel: %p%nrooter: %C.%M(%c{1}.java:%L) %nmessage: %m%n
# Use this layout for LogFactor 5 analysis
# 应用于文件回滚
log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender
log4j.appender.ROLLING_FILE.Threshold=DEBUG
#webapp.root变量为spring设定的,指向当前应用的根目录,绝对路径
log4j.appender.ROLLING_FILE.File=${webapp.root}/WEB-INF/logs/JFrame.log
log4j.appender.ROLLING_FILE.Append=true
#log4j.appender.ROLLING_FILE.Encoding=GBK
log4j.appender.ROLLING_FILE.MaxFileSize=1MB
log4j.appender.ROLLING_FILE.MaxBackupIndex=10
log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.ROLLING_FILE.layout.ConversionPattern=------------------ %d{yyyy-MM-dd HH:mm:ss} ------------------%nlevel: %p%nrooter: %C.%M(%c{1}.java:%L) %nmessage: %m%n
#应用于循环文件
#log4j.appender.R=org.apache.log4j.RollingFileAppender
#log4j.appender.R.File=${webapp.root}/WEB-INF/logs/JFrame.log
#log4j.appender.R.MaxFileSize=100KB
#log4j.appender.R.MaxBackupIndex=10
#log4j.appender.R.layout=org.apache.log4j.PatternLayout
#log4j.appender.R.layout.ConversionPattern=------------------ %d{yyyy-MM-dd HH:mm:ss} ------------------%nlevel: %p%nrooter: %C.%M(%c{1}.java:%L) %nmessage: %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.BufferSize=10
#[email protected]
#log4j.appender.MAIL.SMTPHost=www.vondef.com
#log4j.appender.MAIL.Subject=Log4J Message
#[email protected]
#log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout
#log4j.appender.MAIL.layout.ConversionPattern=[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 = [email protected]
#log4j.appender.im.layout=org.apache.log4j.PatternLayout
##log4j.appender.im.layout.ConversionPattern =[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
然后,在你的web.xml中配置如下东东
<!--由Sprng载入的Log4j配置文件位置-->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<!--Spring log4j Config listener-->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!-- 必须放在classpath下,不然启动的时候会有一两个警告信息,看着很不爽,不过也不影响后面的使用 -->
如果没有采用spring,可以自己编写一个listerner获取context-param参数,
然后将参数set到System中去。
这样在log4j.properties中就可以${xxx}方式引用该变量了。