It just takes 15 seconds. Send your
details and we’ll get back to you soon.

Log4j: Creating Different Log Files Every Day

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

One Response to Log4j: Creating Different Log Files Every Day

  1. It’s really a great and useful piece of info. I’m happy that you just shared this helpful information with us. Please keep us up to date like this. Thanks for sharing.

Leave a Reply

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

4 + 3 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>