Skip to content

Log4j2 RollingFileAppender with TimeBasedTriggeringPolicy fails to delete old logs on application restart #196

@liaodn

Description

@liaodn

I have observed that old log files are not being automatically deleted, leading to disk space exhaustion over time, especially when the BifroMQ service is restarted frequently.
The logging configuration uses a RollingFile Appender with a TimeBasedTriggeringPolicy(e.g., rolling every hour) and a DefaultRolloverStrategy with max attribute set to limit the number of files. This configuration works correctly during continuous runtime: as new log files are created, the oldest files beyond the max limit are deleted.
However, upon service restart, this cleanup process fails. The appender starts logging to a new file (e.g., info.log), but all previous log files matching the file pattern (e.g., info.2024-05-20_*.log) are left untouched, regardless of the max setting. This results in the accumulation of log files that are never purged.

Here is the original configuration.
<Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> </Policies> <DefaultRolloverStrategy max="7"/>

Recommended configuration, It works
<Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> <OnStartupTriggeringPolicy /> </Policies> <DefaultRolloverStrategy> <Delete basePath="${logDir}" maxDepth="1"> <IfFileName glob="info.*.log" /> <IfLastModified age="72h" /> </Delete> </DefaultRolloverStrategy>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions