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 backup of log files are 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. Log4j2 RollingFileAppender example – 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. log4j2.properties
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. log4j2.xml
<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. RollingFileAppender – rollover based on date time
We can roll over log files based on date time as well.
3.1. RollingFileAppender example
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. Daily roll over logs example
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. RollingFileAppender – rollover based on both – log size and date time
If you want to rollover log files based on file size and date time both, then you 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:
Vincent Rivoire
Example 3.2. Daily rollover logs example does not even load…
Lokesh Gupta
Please verify it again.
QuestionForYou
Example 3.1 sets the file name pattern twice – the fileNamePattern attribute in RollingFile and the fileNamePattern attribute in TimeBasedTriggeringPolicy. Is this necessary? Whats the difference between the two?