Log4j2 RollingFileAppender

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 rollover the file.

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

1. Log4j2 Maven Dependencies


<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>

Check the latest version in maven repository.

2. SizeBasedTriggeringPolicy – Rolling over 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 rolling file appender in log4j.properties in 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

logger.rolling.name = rollingFile
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile

2.2. XML Configuration

<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" />
</RollingFile>

3. TimeBasedTriggeringPolicy – Rolling over 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 time.

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

For example, pattern '{MM-dd-yyyy-HH}' will rollover 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 rollover logs on daily basis, set 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 both, then we need to use SizeBasedTriggeringPolicy and TimeBasedRollingPolicy both.

In given example, appender is able to refer the file name pattern and time based rollover strategy using filePattern attribute which include {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>

Happy Learning !!

References:

RollingFileAppender Java Doc

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.