Log4j分开日志文件

时间:2012-10-09 11:05:36

标签: java log4j

  

可能重复:
  Creating multiple log files of different content with log4j

我想将调试日志和信息日志存储在单独的文件中,任何人都可以给我样本。

log4j.rootLogger=debug,R1,R2

log4j.appender.R2=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R2.File=/Test/ex/ex.log
log4j.appender.R1.DatePattern='.'dd-MM-yy
log4j.appender.R2.layout=org.apache.log4j.PatternLayout
log4j.appender.R2.layout.ConversionPattern=%d{dd/MM/yy kk:mm:ss.SSS} %-5p [%t] %x (%F:%L) - %m%n

当我尝试上述操作时,调试和信息日志存储在示例文件中。但是如何单独存储。

1 个答案:

答案 0 :(得分:1)

关键是使用log4j提供的LevelMatchFilter。

此处是配置的更新版本

log4j.rootLogger=debug,R1,R2

log4j.appender.R1.DatePattern='.'dd-MM-yy 
log4j.appender.R1=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R1.File=/Test/error.log
log4j.appender.R1.layout=org.apache.log4j.PatternLayout 
log4j.appender.R1.layout.ConversionPattern=%d{dd/MM/yy kk:mm:ss.SSS} %-5p [%t] %x (%F:%L) - %m
log4j.appender.R1.filter.1=org.apache.log4j.varia.LevelMatchFilter
log4j.appender.R1.filter.1.LevelToMatch=ERROR
log4j.appender.R1.filter.1.AcceptOnMatch=true
log4j.appender.R1.filter.2=org.apache.log4j.varia.DenyAllFilter

log4j.appender.R2.DatePattern='.'dd-MM-yy 
log4j.appender.R2=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R2.File=/Test/info.log
log4j.appender.R2.layout=org.apache.log4j.PatternLayout 
log4j.appender.R2.layout.ConversionPattern=%d{dd/MM/yy kk:mm:ss.SSS} %-5p [%t] %x (%F:%L) - %m
log4j.appender.R2.filter.1=org.apache.log4j.varia.LevelMatchFilter
log4j.appender.R2.filter.1.LevelToMatch=INFO
log4j.appender.R2.filter.1.AcceptOnMatch=true
log4j.appender.R2.filter.2=org.apache.log4j.varia.DenyAllFilter

log4j.appender.debug.DatePattern='.'dd-MM-yy 
log4j.appender.debug=org.apache.log4j.DailyRollingFileAppender
log4j.appender.debug.File=/Test/debug.log
log4j.appender.debug.layout=org.apache.log4j.PatternLayout 
log4j.appender.debug.layout.ConversionPattern=%d{dd/MM/yy kk:mm:ss.SSS} %-5p [%t] %x (%F:%L) - %m
log4j.appender.debug.filter.1=org.apache.log4j.varia.LevelMatchFilter
log4j.appender.debug.filter.1.LevelToMatch=DEBUG
log4j.appender.debug.filter.1.AcceptOnMatch=true
log4j.appender.debug.filter.2=org.apache.log4j.varia.DenyAllFilter

参考文献:   PropertyConfiguration   LevelMatchFilter