Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j Rollingpolicy and MaxbackupIndex

Am using the following code to rollover logs each and every minute and it works perfectly.

log4j.appender.AllFlows=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.AllFlows.rollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.AllFlows.rollingPolicy.File=E:/Logs/AllFlows.log 
log4j.appender.AllFlows.rollingPolicy.FileNamePattern=E:/Logs/AllFlows.log.%d{yyyy-MM-dd-HH-mm}
log4j.appender.AllFlows.MaxBackupIndex=10
log4j.appender.AllFlows.layout=org.apache.log4j.PatternLayout
log4j.appender.AllFlows.layout.ConversionPattern=%d %-5p %x - %m%n

However i just want to know , are they any alternatives for MaxbackupIndex as this is not working as expected when i use TimebasedRollingPolicy?

Am using log41.2.17 and apache log4j extras

like image 204
Ram Avatar asked Feb 20 '15 01:02

Ram


People also ask

What is MaxBackupIndex in log4j?

The MaxBackupIndex option determines how many backup files are kept before the oldest is erased. This option takes a positive integer value. If set to zero, then there will be no backup files and the log file will be truncated when it reaches MaxFileSize .

What is RollingPolicy in log4j?

A RollingPolicy specifies the actions taken on a logging file rollover.

How does log4j RollingFileAppender work?

Log4j2 RollingFileAppender is an OutputStreamAppender that writes log messages to files, following a configured triggering policy about when a rollover (backup) should occur. It also has a configured rollover strategy about how to rollover the file.


1 Answers

Unfortunately, this is not possible using the standard API of log4j or even with the Extras.

However, you can use the class org.apache.log4j.DailyMaxRollingFileAppender1, e.g.:

log4j.appender.AllFlows=org.apache.log4j.DailyMaxRollingFileAppender
log4j.appender.AllFlows.File=E:/Logs/AllFlows.log 
log4j.appender.AllFlows.MaxBackupIndex=10
log4j.appender.AllFlows.DatePattern='.'yyyy-MM-dd-HH-mm
log4j.appender.AllFlows.layout=org.apache.log4j.PatternLayout  
log4j.appender.AllFlows.layout.ConversionPattern=%d %-5p %x - %m%n

Notes

  1. See the code of this class in Custom DailyRollingFileAppender with MaxBackupIndex.
like image 154
Paul Vargas Avatar answered Sep 28 '22 04:09

Paul Vargas