DailyRollingFileAppender
The main intention of DailyRollingFileAppender is to roll files in FileAppender on daily basis. If we want to use DailyRollingFileAppender in log4j applications then we have to use “org.apache.log4j.DailyRolling FileAppender” and we have to use “datePattern” property in properties file.
Note
- If we are using DailyRollingFileAppender in our application then it is not required to use “maxFileSize” and “maxBackupIndex” properties in log4j.properties file.
- The default pattern for datePattern property is ‘.’ ‘.’ yyyy-MM-dd , it will roll over every day midnight
For “datePattern” property we are able to use the following values.
- ‘.’ yyyy-MM : It will roll over at the end of each month and at the beginning of the next
month. - ‘.’ yyyy-MM-dd : It will roll over at midnight each day.
- ‘.’ yyyy-MM-dd-a :It will roll over at midday and midnight of each day.
- ‘.’ yyyy-MM-dd-HH : It will roll over at the top of every hour.
- ‘.’ yyyy-MM-dd-HH-mm : It will roll over every minute.
- ‘.’ yyyy-ww : It will roll over on the first day of each week depending upon the locale
log4j.properties
log4j.rootLogger = ALL, FILE
FileAppender Configuration
log4j.appender.FILE = org.apache.log4j.DailyRollingFileAppender log4j.appender.FILE.file= /home/ashok/loggers/ashok.log log4j.appender.FILE.DatePattern='.' yyyy-MM-dd-HH-mm log4j.appender.FILE.MaxFileSize= 5MB log4j.appender.FILE.MaxBackupIndex= 3 log4j.appender.FILE.layout = org.apache.log4j.PatternLayout log4j.appender.FILE.layout.conversionPattern = %d{yyyy-mm-dd hh:mm:ss } %-c %p : This is %m%n
package com.ashok.log4j; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * * @author ashok.mariyala * */ public class TestLog4J { private static final Logger LOG = LoggerFactory.getLogger(TestLog4J.class); public static void main(String[] args) { LOG.trace("This is trace message"); LOG.debug("This is debug message"); LOG.info("This is info message"); LOG.warn("This is warn message"); LOG.error("This is error message"); } }
DailyRollingFileAppender