Won't delete old log files with RollingFileAppender + OnStartupTriggeringPolicy + DirectWriteRolloverStrategy

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Won't delete old log files with RollingFileAppender + OnStartupTriggeringPolicy + DirectWriteRolloverStrategy

Anders

Hi,

I am using log4j-slf4j-impl version 2.12.1, and an application that will be executed via command line and ternimated after finished.

The application won't be executed more than once at the same time in local host, but it's possible we run it again while previous application is running.

So I'd like to separate logging files by yyyyMMdd-HHmmss, then delete ones if file modified time is elder than specified age (here I set as 1 minute for testing) when application startup.

My log4j2.xml content:

<RollingFile name="default"
             filePattern="${output.folder}/my.%d{yyyyMMdd-HHmmss}.log">
    <PatternLayout pattern="${pattern}" />
    <Policies>
        <OnStartupTriggeringPolicy minSize="0" />
    </Policies>

    <DirectWriteRolloverStrategy>
        <Delete basePath="${output.folder}" maxDepth="1">
            <IfFileName glob="my*.log" />
            <IfLastModified age="1m" />
        </Delete>
    </DirectWriteRolloverStrategy>
</RollingFile>
Logging files are look like below:
  • my.20191127-091410.log
  • my.20191127-091415.log
  • my.20191127-091527.log
  • my.20191127-091533.log
The question is above log4j2 setting is not working.
Old logging files my.20191127-091410.log and  my.20191127-091415.log are not deleted.

I tried to trace code and found something weird.
First, the outputstream (shown as below) will be null because no fileName attribute while using DirectWriteRolloverStrategy.

Second, RollingFileManager doesn't nothing in its own rollover method due to OutputStream is null and hence there is no chance to do rollover of DirectWriteRolloverStrategy (shown as below).

Do I understand in wrong way?
Please let me know if I do something wrong or any suggestion.
Thank you very much.