Log4j 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. Log4j maven dependencies
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
2. RollingFileAppender example – rollover based on log file size
This given configuration roll over th 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. log4j.properties
We can configure rolling file appender in log4j.properties in given way.
log4j.appender.rollingFile=org.apache.log4j.RollingFileAppender log4j.appender.rollingFile.File=${LOG_DIR}/application.log log4j.appender.rollingFile.layout=org.apache.log4j.PatternLayout log4j.appender.rollingFile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n log4j.appender.rollingFile.MaxFileSize=10MB log4j.appender.rollingFile.MaxBackupIndex=5 log4j.appender.rollingFile.append=true log4j.rootCategory=ALL, rollingFile
2.2. log4j.xml
<appender name="rollingFile" class="org.apache.log4j.RollingFileAppender"> <param name="file" value="${LOG_DIR}/application.log" /> <param name="MaxFileSize" value="10MB" /> <param name="MaxBackupIndex" value="5" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n" /> </layout> </appender>
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 '{dd-MMM}'
will rollover log file everyday of month. Similarly, '{MM-dd-yyyy-HH}'
will rollover every hour.
We also use .gz
extension so log4j will compress the log file automatically.
<appender name="rollingFile" class="org.apache.log4j.rolling.RollingFileAppender"> <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy"> <param name="FileNamePattern" value="${LOG_DIR}/application.%d{dd-MMM}.log.gz" /> </rollingPolicy> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p - %m%n" /> </layout> </appender>
3.2. DailyRollingFileAppender example
To enable the daily rolling, log4j provides DailyRollingFileAppender
which extends FileAppender
. Use it directly, if you want to roll over your log files daily.
<appender name="rollingFile" class="org.apache.log4j.rolling.DailyRollingFileAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p - %m%n" /> </layout> </appender>
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.
<appender name="rollingFile" class="org.apache.log4j.rolling.RollingFileAppender"> <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy"> <param name="ActiveFileName" value="${LOG_DIR}/application.log" /> <param name="FileNamePattern" value="${LOG_DIR}/application.%d{dd-MMM}.log.gz" /> </rollingPolicy> <triggeringPolicy class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy"> <param name="MaxFileSize" value="10MB" /> </triggeringPolicy> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p - %m%n" /> </layout> </appender>
References:
Log4j Rolling File Appender Java Doc
Vasudev Patil
How to configure TimeBasedRollingPolicy using Properties file for automatic Compression
Deepak
I want to roll over the files every 8 hours … Is this possible with the help of org.apache.log4j.rolling.TimeBasedRollingPolicy.
Pankaj
Has this been tested?
Because there is no package ‘rolling’ at that path, nor is there any class ‘TimeBasedRollingPolicy’ in log4j 1.2.17 version (or atleast I could not find it).
Can you please correct this?
Lokesh Gupta
Its tested. Please refer to this link.
Deepak
org.apache.log4j.rolling.RollingFileAppender is present in apache-log4j-extras-1.2.17.jar
Wasim Shaikh
Please give the property configuration for
Wasim Shaikh
Please provide the java configuration for below
Raul Brun Sotelo
Hi,
I think you made a mistake, the class DailyRollingFileAppender extends from FileAppender, not from RollingFilleAppender, at least in current version 1.2.
This can originate some problems if you want to manage backup files with DailyRollingFileAppender.
Lokesh Gupta
Thanks for pointing out. I updated the post.
Maik
Hi, first, u wrote a very good tutorial, thanks!
I think, there is a mistake:
class=”org.apache.log4j.rolling.RollingFileAppender”
the package org.apache.log4j.rolling is not available, so it should be class=”org.apache.log4j.RollingFileAppender” Am I right?