How to configure log4j for creating daily rolling log files
- Details
- Written by Nam Ha Minh
- Last Updated on 01 July 2019   |   Print Email
It is possible to configure log4j for splitting up a regular log file into many ones based on a specific schedule, such as daily, weekly, monthly, or even hourly, minutely. This technique is called rolling log files. For example, if the daily schedule is used, log4j would create the following log files:
app.log.2012-10-30 app.log.2012-11-01 app.log.2012-11-02 app.log.2012-11-03 … app.log
Each 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-dd
The 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 |
Note that if you want to add literal text to the date pattern, you must escape it in a pair of single quotes, for example:
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.
1. Configure log4j daily rolling log files in properties file
Here is an example of a log4j’s properties configuration file which is configured for daily rolling log files:
# 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
2. Configure log4j daily rolling log files in XML file
Configure daily rolling log files through log4j’s XML configuration file is as following example:
<?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>
3. Configure log4j daily rolling log files in programmatically
Following is an example Java program that configures log4j for daily rolling log files programmatically:
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"); } }
Related Java log4j Tutorials:
- How to configure log4j as logging mechanism in Java
- Log4j common conversion patterns
- How to initialize log4j for Java web application
- How to use log4j in Spring MVC
- How to use log4j in Struts2
Other Java Coding Tutorials:
- 10 Common Mistakes Every Beginner Java Programmer Makes
- 10 Java Core Best Practices Every Java Programmer Should Know
- How to become a good programmer? 13 tasks you should practice now
Comments
Can you tell me alternative Idea
Same implementation is done on windows server and it looks good.
Please advise me like why the behavior of rolling is different from OS specified?