Log4j: Creating Different Log Files Every Day

September 5, 2012 by Gunpreet | 1 comments

To avoid getting log messages in single log file we can create a new log file every day in just one step. This can be done very easily by using DailyRollingFileAppender instead of RollingFileAppender . We just need to add a new Log4J appenders to our configuration file (Config.groovy).

In the following example we create a DailyRollingFileAppender in our configuration so each day a new log file is created and old log files are renamed with the date in the file.

import org.apache.log4j.DailyRollingFileAppender

log4j = {
    appenders {
        console name: 'stdout', layout: pattern(conversionPattern: '%c{2} %m%n')
             appender new DailyRollingFileAppender(
                  name: 'stacktrace',
                  datePattern: "'.'yyyy-MM-dd",
                  fileName: logFilePath+'/FileName.log',  //storage path of log file           
                  layout: pattern(conversionPattern:'%d [%t] %-5p %c{2} %x - %m%n')

For this you just need to import DailyRollingFileAppender class

Hope this will help you. :-)
Gunpreet Bedi


Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our Blog

Get latest articles straight to your inbox. Enter your email address below: