app.log.2012-10-30 app.log.2012-11-01 app.log.2012-11-02 app.log.2012-11-03 … app.logEach log file is rolled out every day, and the file without date in its name is the current log file. Suppose today is 2012-11-04, and at midnight, log4j will back up the app.logfile into app.log.2012-11-04, and the app.log file become logging for the new day, 2012-11-05, and so on. This is very useful when there is a need for tracking log files based on some interval of time. It is also helps to identify problem quickly by inspecting only the relevant log files.In order to implement daily rolling log files, log4j provides the DailyRollingFileAppender class, which is inheriting from FileAppender class. To use this appender, we need to specify log file name and a date pattern, for example:
log4j.appender.Appender2=org.apache.log4j.DailyRollingFileAppender log4j.appender.Appender2.File=app.log log4j.appender.Appender2.DatePattern='.'yyyy-MM-ddThe following table shows all the date patterns defined by log4j for daily rolling log files:
Schedule | DatePattern | Example of log file’s name |
Minutely |
| app.log.2012-11-04-21-54 |
Hourly |
| app.log.2012-11-04-22 |
Half-daily |
| app.log.2012-11-04-AM app.log.2012-11-04-PM |
Daily |
| app.log.2012-11-04 |
Weekly |
| app.log.2012-45 app.log.2012-46 |
Monthly |
| app.log.2012-10 app.log.2012-11 |
log4j.appender.Appender2.DatePattern=’_’yyyy-MM-dd’.log’Let’s see how to implement rolling log files in log4j with different configurations: properties file, xml file, and programmatically.
# LOG4J daily rolling log files configuration log4j.rootLogger=DEBUG, RollingAppender log4j.appender.RollingAppender=org.apache.log4j.DailyRollingFileAppender log4j.appender.RollingAppender.File=app.log log4j.appender.RollingAppender.DatePattern='.'yyyy-MM-dd log4j.appender.RollingAppender.layout=org.apache.log4j.PatternLayout log4j.appender.RollingAppender.layout.ConversionPattern=[%p] %d %c %M - %m%n
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="RollingAppender" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="app.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%p] %d %c %M - %m%n"/>
</layout>
</appender>
<root>
<priority value="DEBUG"/>
<appender-ref ref="RollingAppender" />
</root>
</log4j:configuration> import org.apache.log4j.Appender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.DailyRollingFileAppender;
public class ProgrammaticDailyRollingLogFilesExample {
public static void main(String[] args) {
// creates pattern layout
PatternLayout layout = new PatternLayout();
String conversionPattern = "[%p] %d %c %M - %m%n";
layout.setConversionPattern(conversionPattern);
// creates daily rolling file appender
DailyRollingFileAppender rollingAppender = new DailyRollingFileAppender();
rollingAppender.setFile("app.log");
rollingAppender.setDatePattern("'.'yyyy-MM-dd");
rollingAppender.setLayout(layout);
rollingAppender.activateOptions();
// configures the root logger
Logger rootLogger = Logger.getRootLogger();
rootLogger.setLevel(Level.DEBUG);
rootLogger.addAppender(rollingAppender);
// creates a custom logger and log messages
Logger logger = Logger.getLogger(ProgrammaticDailyRollingLogFilesExample.class);
logger.debug("this is a debug log message");
logger.info("this is a information log message");
logger.warn("this is a warning log message");
}
}
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.