log4j-extras MaxBackupIndex or similar

2019-06-20 08:35发布

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条回答
仙女界的扛把子
2楼-- · 2019-06-20 09:33

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

查看更多
登录 后发表回答