Log4j Rollingpolicy and MaxbackupIndex
Asked Answered
E

2

5

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

Exclusion answered 20/2, 2015 at 1:37 Comment(1)
there is solution using custom log appender - https://mcmap.net/q/299093/-how-can-i-get-log4j-to-delete-old-rotating-log-filesProvide
R
5

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.DailyMaxRollingFileAppender 1, 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.
Rustyrut answered 25/2, 2015 at 23:30 Comment(1)
okay. can you please tell me which log4j jar version has DailyMaxRollingFileAppender? 1.2.17?Exclusion
L
0

It is possible using DefaultRolloverStrategy oin log4j2

<RollingFile name="LogFile" fileName="/logs/application.log" immediateFlush="false" append="false"
                         filePattern="//logs/abc-%d{MM-dd-yyyy}-%i.log.gz">
    <PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} %m%n"/>
    <Policies>
        <TimeBasedTriggeringPolicy/> <!-- Above pattern will allow the files to be rolled over at midnight -->
    </Policies>
    <DefaultRolloverStrategy max="5"/>
</RollingFile>
Liquescent answered 9/9, 2019 at 14:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.