Understanding Log4j2 RollingFile Appender: TriggeringPolicy, RolloverStrategy, and DeleteAction
This article explains how Log4j2's RollingFileAppender works, covering the roles of TriggeringPolicy and RolloverStrategy, showing various filePattern configurations, detailing the DefaultRolloverStrategy max parameter, introducing DeleteAction for custom file cleanup, and providing Java demo code and test configurations.
Log4j2 provides the RollingFileAppender to automatically roll over log files when certain conditions are met, such as reaching a specified size or time interval.
The rollover process is driven by two concepts: TriggeringPolicy , which decides when a rollover should occur, and RolloverStrategy , which decides how the rollover is performed. Log4j2 ships with a default DefaultRolloverStrategy .
An example log4j2.xml configuration demonstrates a basic RollingFile setup:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<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>The filePattern can contain a date/time pattern (e.g., yyyy-MM-dd HH ) and/or an integer counter %i . The pattern determines the naming of archived files.
Log4j2 supports several TriggeringPolicy types:
CronTriggeringPolicy – triggers based on a cron expression.
OnStartupTriggeringPolicy – triggers when the JVM starts.
SizeBasedTriggeringPolicy – triggers when the log file reaches a configured size (e.g., 20MB ).
TimeBasedTriggeringPolicy – triggers when the current time no longer matches the date/time pattern in filePattern .
CompositeTriggeringPolicy – combines multiple policies, such as size‑and‑time based triggers.
Typical usage focuses on SizeBasedTriggeringPolicy and TimeBasedTriggeringPolicy , either alone or together via CompositeTriggeringPolicy .
The DefaultRolloverStrategy includes a max attribute that limits the number of archived files when a %i counter is present. If the filePattern contains only a date/time pattern, max has no effect; if it contains only %i , the counter increments until max is reached and older files are deleted; if both are present, the counter resets when the date/time portion rolls over.
Three common filePattern scenarios are illustrated with XML snippets:
<RollingFile fileName="logs/app.log" filePattern="logs/app-%d{yyyy-MM-dd}.log"> ... </RollingFile> <RollingFile fileName="logs/app.log" filePattern="logs/app-%i.log"> ... </RollingFile> <RollingFile fileName="logs/app.log" filePattern="logs/app-%d{yyyy-MM-dd HH-mm}-%i.log"> ... </RollingFile>Since Log4j 2.5, DeleteAction allows custom cleanup of archived logs. A sample configuration shows how to delete files matching app-*.log.gz that are older than 60 days:
<DefaultRolloverStrategy>
<Delete basePath="${baseDir}" maxDepth="2">
<IfFileName glob="*/app-*.log.gz"/>
<IfLastModified age="60d"/>
</Delete>
</DefaultRolloverStrategy>A simple Java demo program logs incremental numbers to trigger rollovers:
public class HelloWorld {
public static void main(String[] args) {
Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
try {
for (int i = 0; i < 50000; i++) {
logger.info("{}", i);
logger.info("logger.info\n");
Thread.sleep(100);
}
} catch (InterruptedException e) {}
}
}Additional test configurations illustrate:
Time‑based rollover every 5 seconds using TimeBasedTriggeringPolicy interval="5" .
Size‑based rollover when the file reaches 5KB using SizeBasedTriggeringPolicy size="5KB" .
Combination of time‑based and size‑based triggers with a max="3" counter and %i in the pattern.
The article concludes with a brief thank‑you note and references to the original source.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.