1

I used the following Log4j2 configuration to test the behavior. It rotates the file every 10 seconds and should keep only 3 files. However no file is deleted. What am I missing?

Please don't suggest to use the delete action.

<RollingFile name="File" fileName="/var/log/mylog.log" filePattern="/var/log/mylog-%d{yyyy-MM-dd-HH-mm-ss}.log.gz">
        <PatternLayout>
            <Pattern>${LOG_PATTERN}</Pattern>
        </PatternLayout>
        <Policies>
            <TimeBasedTriggeringPolicy interval="10"/>
        </Policies>
        <DefaultRolloverStrategy max="3"/>
    </RollingFile>

1 Answer 1

3

I realise this is an old question, but I had the same problem.

I also realise the OP said "Please don't suggest to use the delete action" so this may mean there are version restrictions on using Delete.

However I found this feature request which sheds some light on this behaviour - a comment there indicates.

DefaultRolloverStrategy max attribute only applies if you have a %i in the file pattern.

The manual, under "Default Rollover Strategy", indicates

The default rollover strategy accepts both a date/time pattern and an integer from the filePattern attribute specified on the RollingFileAppender itself

and

max : integer : "The maximum value of the counter. Once this values is reached older archives will be deleted on subsequent rollovers. The default value is 7."

A bit a mix of "counter" and "integer" - but think (now) that it means this only applies to "%i" pattern. If the manual used "integer counter" it may be clearer.

A solution I got working (using 2.10.0 and days rather than seconds) using Delete (sorry OP) was:

<RollingFile name="MyLog" fileName="${sys:dataDir}/mylog.log" append="true">
    <FilePattern>./mylog-%d{yyyy-MM-dd}.log.zip</FilePattern>
    <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level - %msg %logger{36}%n"/>
    <Policies>
        <TimeBasedTriggeringPolicy interval="1" modulate="true" />
    </Policies>
    <DefaultRolloverStrategy>
        <Delete basePath="${sys:dataDir}" maxDepth="1">
            <IfFileName glob="mylog-*.log.zip" />
            <IfLastModified age="7d" />
        </Delete>
    </DefaultRolloverStrategy>
</RollingFile >

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.