log4j:使用TimeBasedRollingPolicy使用DailyRollingFileAppender将日志滚动到.gz文件

时间:2011-11-25 04:02:29

标签: log4j

当前系统按预期工作,日志文件按小时滚动一次:

log4j.appender.oozie=org.apache.log4j.DailyRollingFileAppender
log4j.appender.oozie.DatePattern='.'yyyy-MM-dd-HH
log4j.appender.oozie.File=${oozie.log.dir}/oozie.log
log4j.appender.oozie.Append=true
log4j.appender.oozie.layout=org.apache.log4j.PatternLayout
log4j.appender.oozie.layout.ConversionPattern=%d{ISO8601} %5p %c{1}:%L - %m%n

我可以理解,使用TimeBasedRollingPolicy可以使用RollingFileAppender(基于文件大小滚动)将日志文件直接滚动到压缩文件(.gz或.zip)中。我正在使用DailyRollingFileAppender(基于时间的滚动)并希望用它实现压缩。我按如下方式修改了我的属性。但那不起作用。

log4j.appender.oozie=org.apache.log4j.DailyRollingFileAppender
log4j.appender.oozie.DatePattern='.'yyyy-MM-dd-HH
log4j.appender.oozie.File=${oozie.log.dir}/oozie.log
log4j.appender.oozie.Append=true
log4j.appender.oozie.RollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.oozie.RollingPolicy.FileNamePattern=foo.%d{yyyy-MM-dd-HH}.gz
log4j.appender.oozie.layout=org.apache.log4j.PatternLayout
log4j.appender.oozie.layout.ConversionPattern=%d{ISO8601} %5p %c{1}:%L - %m%n

任何指针都将受到高度赞赏。以下是一些相关链接。

Configuring RollingFileAppender in log4j

http://logging.apache.org/log4j/companions/extras/apidocs/org/apache/log4j/rolling/TimeBasedRollingPolicy.html

2 个答案:

答案 0 :(得分:2)

原谅我的意料,但是你在上面给出的确切链接上写道:

  

请注意,TimeBasedRollingPolicy只能配置xml,而不能配置log4j.properties

您是否尝试过将配置重写为XML格式?

答案 1 :(得分:2)

请尝试以下操作,它对我有用:

log4j.rootLogger=CONSOLE,file


log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE..layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

log4j.appender.file=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.file.Threshold=WARN
log4j.appender.file.Encoding=UTF-8

log4j.appender.file.File=/path to your logs/logs/log_file.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.RollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.file.RollingPolicy.FileNamePattern=/path to your                 logs/logs/log_file%d{yyyy-MM-dd-HH-mm-ss}.log
log4j.appender.file.layout.ConversionPattern=%d{MM-dd@HH:mm:ss} %-5p (%13F:%L) %3x - %m%n