I'm using log4j-extras (1.2.17) org.apache.log4j.rolling.RollingFileAppender
with a org.apache.log4j.rolling.TimeBasedRollingPolicy
that rolls daily. Is there a similar property to maxBackupIndex in log4j's org.apache.log4j.RollingFileAppender
(note the package difference) to limit the number of archived files? If not, is there another alternative for daily rolling with limited files?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
If you want to limit the number of file created by log4j then use the DefaultRolloverStrategy and set the Max to the number of files you want to store. But on the generation of new logs the older files will be deleted.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<RollingFile name="RollingFile" fileName="logs/app.log"
filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="250 MB"/>
</Policies>
<DefaultRolloverStrategy max="20"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
Hope that helps you