log4j-extras MaxBackupIndex or similar

巧了我就是萌 提交于 2020-01-03 10:22:10

问题


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



来源:https://stackoverflow.com/questions/23945373/log4j-extras-maxbackupindex-or-similar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!