Rolling File Appender Threshold
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.log file 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='.'
Configuring RollingFileAppender in log4j. I suggest setting a logging threshold Rolling File Appender that compresses old files.
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 '.' Yyyy-MM-dd-HH-mm app. Simple C Program For Trapezoidal Rule there. log.2012-11-04-21-54 Hourly '.' Yyyy-MM-dd-HH app.log.2012-11-04-22 Half-daily '.' Yyyy-MM-dd-a app.log.2012-11-04-AM app.log.2012-11-04-PM Daily '.'
Yyyy-MM-dd app.log.2012-11-04 Weekly '.' Yyyy-ww app.log.2012-45 app.log.2012-46 Monthly '.' Yyyy-MM 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. Configure 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='.'