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.
[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