Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure log4j org.apache.log4j.rolling.RollingFileAppender so that compressed logs are removed once the number of logs passes a set index amount

I am using the org.apache.log4j.rolling.RollingFileAppender in the apache-log4j-extras package to compress the log once they are no longer the active log. Here is an example appender:

<appender name="TRACELOG" class="org.apache.log4j.rolling.RollingFileAppender">
    <param name="file" value="logFileName.log" />

    <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
        <param name="FileNamePattern" value="logFileName.%d.log.gz" />
    </rollingPolicy>

    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d %-5p [%t] %C{2} (%F:%L) - %m%n" />
    </layout>

    <filter class="org.apache.log4j.varia.LevelMatchFilter">
        <param name="LevelToMatch" value="trace" />
        <param name="AcceptOnMatch" value="true" />
    </filter>
    <filter class="org.apache.log4j.varia.DenyAllFilter" />
</appender>

What I would like to do is add something like <param name="MaxBackupIndex" value="14" /> so that it will essentially only keep 14 days worth of compressed logs, but when I try and use it, I get the following warning:

log4j:WARN No such property [maxBackupIndex] in org.apache.log4j.rolling.RollingFileAppender.
like image 665
Brett VanderVeen Avatar asked Feb 15 '23 05:02

Brett VanderVeen


2 Answers

in extra rolling file appender (not the standard) there is no way to specify the rolling policy directly in the appender, cause it's specified in the rolling policy field.

The problem is that you're using a TimeBasedRollingPolicy that doesn't support maxBackupIndex, maxBackupIndex is only supported in FixedWindowRollingPolicy

like image 127
Emanuele Avatar answered Feb 17 '23 20:02

Emanuele


You can write your custom file appender, which extends RollingFileAppender. Here is an example: http://www.javaworld.com/community/node/6148

like image 41
stan Avatar answered Feb 17 '23 21:02

stan