具有文件大小限制的Log4j TimeBasedRollingPolicy

时间:2014-11-24 04:07:52

标签: java log4j

log4j.rootLogger=warn, stdout, R
log4j.appender.R=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.R.rollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.R.triggeringPolicy=org.apache.log4j.rolling.SizeBasedTriggeringPolicy
log4j.appender.R.triggeringPolicy.MaxFileSize=1024
log4j.appender.R.rollingPolicy.FileNamePattern=example.%d{yyyyMMdd}.log.gz
log4j.appender.R.rollingPolicy.ActiveFileName=example.log

日志记录工作正常,但日志文件大小不限于1024.它继续增加。

我想设置限制文件大小限制。请建议。

1 个答案:

答案 0 :(得分:0)

我认为在使用TimeBasedRollingPolicy时我们没有设置大小限制。 我已经开始使用以下解决方案来解决问题..

log4j.appender.R=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.R.Append=true
log4j.fileName=logs/example
log4j.appender.R.File=${log4j.fileName}.log
log4j.appender.R.RollingPolicy=org.apache.log4j.rolling.FixedWindowRollingPolicy
log4j.appender.R.RollingPolicy.FileNamePattern=${log4j.fileName}.%i.log.gz
log4j.appender.R.RollingPolicy.MinIndex=1
log4j.appender.R.RollingPolicy.MaxIndex=13
log4j.appender.R.TriggeringPolicy=org.apache.log4j.rolling.SizeBasedTriggeringPolicy
log4j.appender.R.TriggeringPolicy.MaxFileSize=400000000
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d [%t] %-5p %c <%X{currentUser}> - %m%n