Understanding Log4j2 RollingFileAppender: Configuration, Policies, and Strategies

This article explains how Log4j2's RollingFileAppender works, covering the concepts of rollover, the various TriggeringPolicy options, DefaultRolloverStrategy behavior, DeleteAction configuration, and provides sample XML configurations and a Java demo to illustrate time‑based, size‑based, and composite rollover scenarios.

Code Ape Tech Column
Code Ape Tech Column
Code Ape Tech Column
Understanding Log4j2 RollingFileAppender: Configuration, Policies, and Strategies

RollingFileAppender is a Log4j2 component that enables log file rollover (archiving) when specified conditions such as file size or time are met.

Rollover is triggered by a TriggeringPolicy; when the policy conditions are satisfied, the current log file is renamed according to the filePattern and a new file is created for further logging.

Supported TriggeringPolicy types include TimeBasedTriggeringPolicy, SizeBasedTriggeringPolicy, CronTriggeringPolicy, OnStartupTriggeringPolicy, and CompositeTriggeringPolicy (a mix of policies).

The default DefaultRolloverStrategy controls how many archived files are retained via its max attribute. Its effect depends on the composition of filePattern —whether it contains only a date/time pattern, only an index (%i), or both.

Since Log4j 2.5, DeleteAction allows custom deletion rules. By configuring a Delete element with IfFileName and IfLastModified conditions, users can specify which archived files to remove and under what age criteria.

Example configuration for a basic RollingFileAppender:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp">
  <Appenders>
    <RollingFile name="RollingFile" fileName="logs/app.log"
                 filePattern="logs/app-%d{yyyy-MM-dd HH}.log">
      <PatternLayout><Pattern>%d %p %c{1.} [%t] %m%n</Pattern></PatternLayout>
      <Policies>
        <TimeBasedTriggeringPolicy interval="1"/>
        <SizeBasedTriggeringPolicy size="250MB"/>
      </Policies>
    </RollingFile>
  </Appenders>
  <Loggers>
    <Root level="error">
      <AppenderRef ref="RollingFile"/>
    </Root>
  </Loggers>
</Configuration>

More complex examples combine time‑based and size‑based policies, use an index (%i) in the pattern, and demonstrate how the max attribute interacts with the index to limit the number of archived files.

A Java demo program writes log entries in a loop, sleeping between writes, to show how rollovers occur when the file reaches a size threshold or when the time interval expires. The demo also illustrates the effect of the max parameter together with the %i counter.

Overall, the article provides a comprehensive guide to configuring Log4j2 RollingFileAppender, selecting appropriate triggering policies, managing archived files with strategies and delete actions, and testing the configuration with sample code.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaConfigurationlogginglog4j2RollingFile
Code Ape Tech Column
Written by

Code Ape Tech Column

Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.