Log4j: Creating Different Log Files Every Day

05 / Sep / 2012 by Gunpreet 0 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.

[java]

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
maxFileSize:1024,
layout: pattern(conversionPattern:’%d [%t] %-5p %c{2} %x – %m%n’)
)
}
}
[/java]

For this you just need to import DailyRollingFileAppender class

Hope this will help you. 🙂
Gunpreet Bedi
gunpreet@intelligrape.com

https://twitter.com/gunpreet_ginny

FOUND THIS USEFUL? SHARE IT

Leave a Reply

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