我正在使用log4j-1.2.16.jar和apache-log4j-extras-1.2.17.jar。
我想启用带有日期附加到文件名的滚动文件。但是显示这个错误并且文件没有按预期旋转。
我的log4j.properties文件看起来像这样。
zookeeper.root.logger=DEBUG, ROLLINGFILE
zookeeper.console.threshold=DEBUG
zookeeper.log.dir=/path/to/logs
zookeeper.log.file=zookeeper.log
zookeeper.log.threshold=DEBUG
zookeeper.tracelog.dir=.
zookeeper.tracelog.file=zookeeper_trace.log
log4j.appender.ROLLINGFILE=org.apache.log4j.RollingFileAppender
log4j.appender.ROLLINGFILE.Threshold=${zookeeper.log.threshold}
log4j.appender.ROLLINGFILE.File=${zookeeper.log.dir}/${zookeeper.log.file}
log4j.appender.ROLLINGFILE.rollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.ROLLINGFILE.rollingPolicy.fileNamePattern=${zookeeper.log.dir}/app%d{yyyy-MM-dd}.log
log4j.appender.ROLLINGFILE.MaxFileSize=20KB
log4j.appender.ROLLINGFILE.MaxBackupIndex=100000
log4j.appender.ROLLINGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.ROLLINGFILE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L] - %m%n
答案 0 :(得分:4)
管理修复它。问题出在org.apache.log4j.RollingFileAppender
上。一旦我将其更改为org.apache.log4j.rolling.RollingFileAppender
zookeeper.root.logger=DEBUG, ROLLINGFILE
zookeeper.console.threshold=DEBUG
zookeeper.log.dir=/path/to/logs
zookeeper.log.file=zookeeper.log
zookeeper.log.threshold=DEBUG
zookeeper.tracelog.dir=.
zookeeper.tracelog.file=zookeeper_trace.log
log4j.appender.ROLLINGFILE=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.ROLLINGFILE.Threshold=${zookeeper.log.threshold}
log4j.appender.ROLLINGFILE.File=${zookeeper.log.dir}/${zookeeper.log.file}
log4j.appender.ROLLINGFILE.rollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.ROLLINGFILE.rollingPolicy.fileNamePattern=${zookeeper.log.dir}/app%d{yyyy-MM-dd}.log
log4j.appender.ROLLINGFILE.MaxFileSize=20KB
log4j.appender.ROLLINGFILE.MaxBackupIndex=100000
log4j.appender.ROLLINGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.ROLLINGFILE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L] - %m%n
答案 1 :(得分:0)
查看 org.apache.log4j.rolling.RollingFileAppender 的代码,该代码来自单独的 apache-log4j-extras 包,该解决方案无法正常工作。虽然该类具有 rollingPolicy和rollingPolicy.fileNamePattern 字段,但它没有 maxFileSize 和 maxBackupIndex 字段。您检查错误日志了吗?
在 org.apache.log4j.RollingFileAppender 类中相反。它具有 MaxFileSize 和 maxBackupIndex 字段,但没有 rollingPolicy 和 rollingPolicy.fileNamePattern 字段。只是没有两种口味。