Log4j2 – Delete Old Logs on Rollover

Deleting the old log files after a period of time is a common requirement. If this Log4j2 tutorial, we will learn to configure the 'Delete' action with RollingFileAppender and DefaultRolloverStrategy which will delete the old logs after a certain time.

1. Log4j2 Delete Action

In log4j2, a RollingFileAppender requires a TriggeringPolicy and a RolloverStrategy. The triggering policy determines if a rollover should be performed, while the rollover strategy defines how the rollover should be done.

Before Log4j2 2.5, the max attribute of DefaultRolloverStrategy was the only option to delete logs at rollover time. In the following example, log4j will create upto 10 log files (each 20MB) everyday before starting to rollover.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">

  <Appenders>
    <RollingFile name="RollingFile" fileName="logs/app.log"
                 filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
      <PatternLayout>
        <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
      </PatternLayout>
      <Policies>
        <TimeBasedTriggeringPolicy />
        <SizeBasedTriggeringPolicy size="20MB"/>
      </Policies>

      <DefaultRolloverStrategy max="10"/>

    </RollingFile>
  </Appenders>

  <Loggers>
    <Root level="error">
      <AppenderRef ref="RollingFile"/>
    </Root>
  </Loggers>

</Configuration>

Since Log4j2 2.5, the new Delete action gives users more control over what files are deleted at rollover time as well as delete other log files generated in past.

In the following example, every day at midnight, log4j rollover process will delete log files 30 days old or older inside the baseDir location.

<DefaultRolloverStrategy>
  <Delete basePath="${baseDir}" maxDepth="2">
    <IfFileName glob="*/app-*.log.gz" />
    <IfLastModified age="P30D" />
  </Delete>
</DefaultRolloverStrategy>

2. Keeping Some Logs and Deleting Others

In a few cases, we may not want to delete all log files generated in past; rather we may want to keep some old logs based on either log file size or count.

The following configuration will delete files that match “/app-.log.gz” and are 30 days old or older but keep the most recent 100 GB or the most recent 10 files, whichever comes first.

<DefaultRolloverStrategy max="100">
  <Delete basePath="${baseDir}" maxDepth="2">
    <IfFileName glob="*/app-*.log.gz">
      <IfLastModified age="P30D">
        <IfAny>
          <IfAccumulatedFileSize exceeds="100GB" />
          <IfAccumulatedFileCount exceeds="10" />
        </IfAny>
      </IfLastModified>
    </IfFileName>
  </Delete>
</DefaultRolloverStrategy>

3. Properties Configuration

We can also configure the Delete action in log4j2.properties file in the following manner:

appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 5
appender.rolling.strategy.action.type = Delete
appender.rolling.strategy.action.basepath = ${baseDir}
appender.rolling.strategy.action.maxdepth = 2
appender.rolling.strategy.action.condition.type = IfLastModified
appender.rolling.strategy.action.condition.age = 30D
appender.rolling.strategy.action.PathConditions.type = IfFileName
appender.rolling.strategy.action.PathConditions.glob = */app-*.log.gz

4. Conclusion

In this short Log4j2 tutorial, we learned to delete the old log files on rollover events. We learned to delete older logs based on aggregate log files size and count. We also learned to configure the Delete action using the properties file.

Happy Learning !!

Leave a Reply

0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial