log4j.properties 文件:
log4j.logger.net.sf.hibernate.cache=debug
log4j.rootLogger = error,portal_log,shop_log
log4j.appender.ROLLING_FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ROLLING_FILE.File=/logs/PT.log
log4j.appender.ROLLING_FILE.DatePattern='.'yyyy-MM-dd'.log'
#log4j.appender.ROLLING_FILE.Append=true
#log4j.appender.ROLLING_FILE.MaxFileSize=50MB
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
log4j.logger.portal_log=INFO, portal_log
log4j.appender.portal_log=com.aisino.global.context.common.logs.MyAppender
log4j.appender.portal_log.Append=true
log4j.appender.portal_log.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.portal_log.File=/logs/portal_log.log
log4j.appender.portal_log.Threshold=INFO
log4j.appender.portal_log.layout=org.apache.log4j.PatternLayout
log4j.appender.portal_log.layout.ConversionPattern=%m%n
log4j.additivity.portal_log=false
log4j.logger.shop_log=INFO, shop_log
log4j.appender.shop_log=com.aisino.global.context.common.logs.MyAppender
log4j.appender.shop_log.Append=true
log4j.appender.shop_log.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.shop_log.File=/logs/shop_log.log
log4j.appender.shop_log.Threshold=INFO
log4j.appender.shop_log.layout=org.apache.log4j.PatternLayout
log4j.appender.shop_log.layout.ConversionPattern=%m%n
log4j.additivity.shop_log=false
配置中关键的配置说明是这一句:
log4j.appender.debug.Threshold = INFO
而它的作用是输出INFO级别以上的内容到日志文件中,所以以上pt.log,shop_log.log,portal_log .log文件中都会包含了ERROR级别的文件。
这就比较烦,得到了我所不要想要的日志信息!!!!!
解决办法是:定义自己的Appender类,继承DailyRollingFileAppender,改写针对Threshold 的设置说明(重写针对级别的比较方法)
源代码:
- public boolean isAsSevereAsThreshold(Priority priority) {
- return threshold == null || priority.isGreaterOrEqual(threshold);
- }
重写 isAsSevereAsThreshold(Priority priority)方法:
- public class MyAppender extends DailyRollingFileAppender {
- @Override
- public boolean isAsSevereAsThreshold(Priority priority) {
- //只判断是否相等,而不判断优先级
- return this.getThreshold().equals(priority);
- }
- }
这样,进行唯一判断,只有当Threshold与priority一致时,才进行输出,就实现了真正Log4j按照级别输出日志文件。
备份,仅供参考!!!