Log4j2 RollingFileAppender Example

Log4j2 RollingFileAppender is an OutputStreamAppender that writes log messages to files, following a configured triggering policy about when a rollover (backup) should occur. It also has a configured rollover strategy about how to roll over the file.

Generally, a backup of the log files is created based on file size, current date, or both.

1. Log4j2 Maven Dependencies

Check the latest versions in the maven repository.

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.11.1</version>
</dependency>
 
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.11.1</version>
</dependency>

2. SizeBasedTriggeringPolicy – Rollover based on Log File Size

This given configuration roll over the log files based on log file size. I have configured the log file size to be 10 MB. Change it as per your requirement.

2.1. Properties Configuration

We can configure the rolling file appender in log4j.properties in the given way.

name = PropertiesConfig
 
appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${LOG_DIR}/application.log
appender.rolling.filePattern = ${LOG_DIR}/application.%d{dd-MMM}.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=10MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 5
appender.rolling.strategy.action.type = Delete
appender.rolling.strategy.action.basepath = ${LOG_DIR}
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
appender.rolling.filter.threshold.type = ThresholdFilter
appender.rolling.filter.threshold.level = DEBUG
 
logger.rolling.name = rollingFile
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile

2.2. XML Configuration

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

  <Properties>
    <Property name="LOG_DIR">/logs</Property>
  </Properties>

  <Appenders>
    <RollingFile
      name="rollingFile"
      fileName="${LOG_DIR}/application.log"
      filePattern="${LOG_DIR}/application.%i.log.gz"
      ignoreExceptions="false">
      <PatternLayout>
          <Pattern>%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n</Pattern>
      </PatternLayout>
      <Policies>
          <SizeBasedTriggeringPolicy size="10MB" />
      </Policies>
      <DefaultRolloverStrategy max="5">
        <Delete basePath="${LOG_DIR}" maxDepth="2">
          <IfFileName glob="*/app-*.log.gz" />
          <IfLastModified age="P30D" />
        </Delete>
      </DefaultRolloverStrategy>
    </RollingFile>
  </Appenders>

  <Loggers>
    <Root level="debug">
      <AppenderRef ref="rollingFile"/>
    </Root>
  </Loggers>

</Configuration>

3. TimeBasedTriggeringPolicy – Rollover based on Date and Time

We can roll over log files based on date and time as well.

3.1. Rolling Over Every Hour

If using RollingFileAppender, then use TimeBasedRollingPolicy to specify when to roll over log files based on date and time.

Notice the FileNamePattern property. It defines the name pattern for rolled-over files. In the given example, it will rename the rollover log files with date-month in log file name.

For example, the pattern '{MM-dd-yyyy-HH}' will rollover the log file every hour.

We also use .gz extension, so log4j will compress the log file automatically.

<RollingFile
  name="rollingFile"
  fileName="${LOG_DIR}/application.log"
  filePattern="${LOG_DIR}/application.%d{dd-MMM}.log.gz"
  ignoreExceptions="false">
  <PatternLayout>
      <Pattern>%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n</Pattern>
  </PatternLayout>
  <Policies>
      <TimeBasedTriggeringPolicy filePattern="${LOG_DIR}/application.%d{dd-MMM-hh}.log.gz" />
  </Policies>
  <DefaultRolloverStrategy max="5" />
</RollingFile>

3.2. Rolling Over Every Day

To enable the daily rolling, log4j2 does not DailyRollingFileAppender which was present in earlier log4j. To roll over logs on daily basis, set the interval to 1 in TimeBasedTriggeringPolicy.

<RollingFile
  name="rollingFile"
  fileName="${LOG_DIR}/application.log"
     filePattern="${LOG_DIR}/application.%d{dd-MMM}.log.gz"
  ignoreExceptions="false">
  <PatternLayout>
      <Pattern>%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n</Pattern>
  </PatternLayout>
  <Policies>
      <TimeBasedTriggeringPolicy interval="1"/>
  </Policies>
  <DefaultRolloverStrategy max="5" />
</RollingFile>

4. Rollover based on Log Size and Date Time

If we want to rollover log files based on file size and date time, then we need to use SizeBasedTriggeringPolicy and TimeBasedRollingPolicy both.

In the given example, the appender is able to refer to the file name pattern and time-based rollover strategy using filePattern attribute which includes {dd-MMM}. Size-based rollover will happen at 10 MB.

<RollingFile
  name="rollingFile"
  fileName="${LOG_DIR}/application.log"
  filePattern="${LOG_DIR}/application.%d{dd-MMM}.log.gz"
  ignoreExceptions="false">
  <PatternLayout>
      <Pattern>%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n</Pattern>
  </PatternLayout>
  <Policies>
      <OnStartupTriggeringPolicy />
        <SizeBasedTriggeringPolicy size="10 MB" />
        <TimeBasedTriggeringPolicy />
  </Policies>
  <DefaultRolloverStrategy max="5" />
</RollingFile>

5. Deleting Logs on Rollover

Log4j 2.5 introduced a Delete action to allow more control over what files are deleted at rollover time.

In the following example, all files under the base directory that match the “*/app-*.log.gz” glob and are 30 days old or older are deleted at rollover time.

<Properties>
    <Property name="baseDir">/logs</Property>
</Properties>

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

Prior to version 2.5, file deletion was possible only with the DefaultRolloverStrategy max attribute. In the following example, log4j2 will keep up to 5 files before removing them.

<DefaultRolloverStrategy max="5" />

Note that we can use both strategies together.

Happy Learning !!

References:

RollingFileAppender Java Doc

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