一:Log4j基础
Log4j由三个重要的组件构成:
Loggers:日志信息的优先级
Appenders:日志信息的输出目的地
Layout:日志信息的输出格式
日志信息的优先级从高到低依次是
- OFF
- FATAL
- ERROR
- WARN
- INFO
- DEBUG
- ALL
二:准备工作
log4J本身需要的jar包:log4j-.jar、commons-logging-xxx.jar、slf4j-api-xxx.jar、slf4j-log4j12-xxx.jar
配置E-Mail依赖的jar包:mail.jar 、activation.jar。
三:具体配置
1.整体配置
log4j.logger.com.saleSystem.aop.LogAdvice=INFO,DATABASE
#log4j.logger.org.hibernate.tool.hbm2ddl=WARN
log4j.rootLogger=DEBUG,Console,OneFile,HtmlFile,MAIL
log4j.logger.com.opensymphony.xwork2=WARN
#print sql parameter
log4j.logger.org.hibernate.type.descriptor.sql.BasicBinder= TRACE
#log4j.logger.org.hibernate.type.descriptor.sql.BasicExtractor=TRACE
#log4j.logger.org.hibernate.SQL=DEBUG
#struts
#配置Log4j中包的输出级别
关闭Spring hibernate struts2不必要的日志
log4j.logger.org.apache.struts2=WARN
log4j.logger.org.apache.commons=WARN
log4j.logger.org.apache.cxf=WARN
#spring
log4j.logger.org.springframework = WARN
#Hibernate
log4j.logger.org.hibernate.ps.PreparedStatementCache= WARN
log4j.logger.org.hibernate=WARN
#print to the console
#Appender.Threshold logger's level (DEBUG|ERROR|INFO)
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern= [%p][%d{yyyy -MM-dd HH\:mm\: ss,SSS}][%c]%m[%X{userId}]%n
#OneFile output to file
log4j.appender.OneFile=org.apache.log4j.RollingFileAppender
log4j.appender.OneFile.File=../log/saleSystem.log
log4j.appender.OneFile.MaxFileSize=10MB
log4j.appender.OneFile.Threshold=ALL
#backups file number
log4j.appender.OneFile.MaxBackupIndex = 3
log4j.appender.OneFile.layout=org.apache.log4j.PatternLayout
log4j.appender.OneFile.layout.ConversionPattern= [%p][%d{yyyy -MM-dd HH\:mm\: ss,SSS}][%c]%m%n
#log4j日循环日志需要次日生成
明天才会生成tomcat.log.2010-9-9
今天的日志不会跟日期作为后缀的
log4j.appender.HtmlFile=org.apache.log4j.DailyRollingFileAppender #每天会生成一个日志文件,以.html结尾
log4j.appender.HtmlFile.file=../log/saleSystem_log
log4j.appender.HtmlFile.DatePattern='-'yyyy-MM-dd'.html'
log4j.appender.HtmlFile.layout=org.apache.log4j.HTMLLayout
log4j.appender.HtmlFile.Threshold=ALL
#配置通过邮件通知,最低级别是ERROR,如果需要更低需要重写Log4j的代码
log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender
log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout
log4j.appender.MAIL.Threshold=FATAL
log4j.appender.MAIL.BufferSize=10 #10条一起发
log4j.appender.MAIL.From=Source@qq.com
log4j.appender.MAIL.SMTPHost=smtp.qq.com
log4j.appender.MAIL.Subject=Log4J Message
log4j.appender.MAIL.SMTPDebug=false
log4j.appender.MAIL.SMTPUsername=Source@qq.com
log4j.appender.MAIL.SMTPPassword=password(自己的密码)
log4j.appender.MAIL.To=Target@qq.com
log4j.appender.MAIL.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
#配置输出到数据库
#logger.com.test,把com.saleSystem.aop.LogAdvice java类中的日志输出到数据库DATABASE中,必须置于log4j.rootLogger之上
log4j.logger.com.test=INFO,L1
log4j.logger.com.saleSystem.aop.LogAdvice= INFO,DATABASE
log4j.appender.DATABASE=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.DATABASE.URL=jdbc:mysql://localhost:3306/log4salesystem
log4j.appender.DATABASE.driver=com.mysql.jdbc.Driver
log4j.appender.DATABASE.user=root
log4j.appender.DATABASE.BufferSize=1
log4j.appender.DATABASE.password=19930815
log4j.appender.DATABASE.Threshold=INFO
#数据库的输出语句
log4j.appender.DATABASE.sql=INSERT INTO oper_log(OPER_MAN,OPER_TIME,OPER_MODULE,OPER_OPTION,OPER_CON) VALUES ('%X{OPER_MAN}','%X{OPER_TIME}','%X{OPER_MODULE}','%X{OPER_OPTION}','%m')
log4j.appender.DATABASE.layout=org.apache.log4j.PatternLayout
log4j.appender.DATABASE.layout.ConversionPattern= [framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
2.配置输出到E-Mail
#配置通过邮件通知,最低级别是ERROR,如果需要更低需要重写Log4j的代码
log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender
log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout
log4j.appender.MAIL.Threshold=FATAL
log4j.appender.MAIL.BufferSize=10 #10条一起发
log4j.appender.MAIL.From=Source@qq.com
log4j.appender.MAIL.SMTPHost=smtp.qq.com
log4j.appender.MAIL.Subject=Log4J Message
log4j.appender.MAIL.SMTPDebug=false
log4j.appender.MAIL.SMTPUsername=Source@qq.com
log4j.appender.MAIL.SMTPPassword=password(自己的密码)
log4j.appender.MAIL.To=Target@qq.com
log4j.appender.MAIL.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
- log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender:配置输出类型
- log4j.appender.MAIL.BufferSize=10:缓存10条,当输出到E-Mail的日志到达10条后,将发送邮件给指定邮箱
- log4j.appender.MAIL.Threshold=FATAL:输出的级别为致命性错误
- log4j.appender.MAIL.From:发送邮件的邮箱
- log4j.appender.MAIL.Subject:邮件主题
- log4j.appender.MAIL.SMTPHost:邮箱服务器
- log4j.appender.MAIL.SMTPUsername:邮箱用户名
- log4j.appender.MAIL.SMTPPassword:邮箱密码
3.输出到数据库
log4j.appender.DATABASE=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.DATABASE.URL=jdbc:mysql://localhost:3306/log4salesystem
#log4j.appender.DATABASE.URL=jdbc:mysql://192.168.1.125:3306/log4salesystem
log4j.appender.DATABASE.driver=com.mysql.jdbc.Driver
log4j.appender.DATABASE.user=root
log4j.appender.DATABASE.BufferSize=1
#log4j.appender.DATABASE.password=12345679
log4j.appender.DATABASE.password=19930815
log4j.appender.DATABASE.sql=INSERT INTO oper_log(OPER_MAN,OPER_TIME,OPER_MODULE,OPER_OPTION,OPER_CON) VALUES ('%X{OPER_MAN}','%X{OPER_TIME}','%X{OPER_MODULE}','%X{OPER_OPTION}','%m')
log4j.appender.DATABASE.Threshold=INFO
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.DATABASE:输出类型
- log4j.appender.DATABASE.URL:输出地址
- log4j.appender.DATABASE.driver:输出驱动,mysql数据库
- log4j.appender.DATABASE.user:数据库用户名
- log4j.appender.DATABASE.password:数据库密码
- log4j.appender.DATABASE.sql:数据库插入语句
- log4j.appender.DATABASE.Threshold:输出级别
- log4j.appender.DATABASE.BufferSize=1 :缓存1条
log4j.logger.com.saleSystem.aop.LogAdvice=INFO,DATABASE
把com.saleSystem.aop.LogAdvicejava类中的日志输出到数据库DATABASE中,必须置于log4j.rootLogger之上
log4j.rootLogger=DEBUG,Console,OneFile,HtmlFile,MAIL
%X{OPER_MAN}、%X{OPER_TIME}','%X{OPER_MODULE}','%X{OPER_OPTION}是怎么来的?
从MDC中得到! MDC内部使用了类似map的机制来存储信息,上下文信息也是每个线程独立地储存,所不同的是信息都是以它们的key值存储在”map”中。相对应的方法,MDC.put(key, value); MDC.remove(key); MDC.get(key);在配置PatternLayout的时候使用:%x{key}来输出对应的value。