Grails with time-based rolling logs

Logging in grails is somehow confusing me a little. I don’t know why but even though I’m familiar with log4j and never had trouble working with it, grails logging sometimes manages to make me feel like a total newbie. After looking for a nice and simple solution for a timebased rolling and archiving, I thought someone else might be interested in it as well and I don’t have to look for it again ;) So here is the daily rolling log configuration, which is also archiving (and zipping) the old log:

Config.groovy

environments {
    ...
    production {
        minify = true


        log4j = {
            def logDirectory = "/var/log/grails"
            def timestamp = "%d [%t] %-5p %c{2} %x - %m%n"

            def debugRollingFile = new RollingFileAppender(name: 'grailsDebugLog', 
                layout: pattern(conversionPattern: timestamp))
            def debugRollingPolicy = new TimeBasedRollingPolicy(fileNamePattern: 
                "${logDirectory}/archived/grails_debug.%d{yyyy-MM-dd}.gz", 
                activeFileName: "${logDirectory}/gibbons_debug.log")
            debugRollingPolicy.activateOptions()
            debugRollingFile.setRollingPolicy(debugRollingPolicy)

            def errorRollingFile = new RollingFileAppender(name: 'grailsErrorLog', 
                layout: pattern(conversionPattern: timestamp))
            def errorRollingPolicy = new TimeBasedRollingPolicy(fileNamePattern: 
                "${logDirectory}/archived/grails_error.%d{yyyy-MM-dd}.gz", 
                activeFileName: "${logDirectory}/gibbons_error.log")
            errorRollingPolicy.activateOptions()
            errorRollingFile.setRollingPolicy(errorRollingPolicy)

            appenders {
                appender debugRollingFile
                appender errorRollingFile
                'null' name: 'stacktrace'
            }

            root {
                info 'grailsDebugLog'
            }
            error 'grailsErrorLog'
        }
    }
    ...
}

As you can see it’s pretty straight forward. On the other hand, this little part of a log4j.properties file is doing the same:

LogfileRoot=/var/log/grails

log4j.rootLogger=INFO, DebugAppender, ErrorAppender

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601} [%t] %-5p %c - %m%n

log4j.appender.DebugAppender=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.DebugAppender.rollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.DebugAppender.rollingPolicy.fileNamePattern=${LogfileRoot}/archived/grails_debug_%d{yyyy-MM-dd}.zip
log4j.appender.DebugAppender.file=${LogfileRoot}/grails_debug.log
log4j.appender.DebugAppender.Threshold=INFO
log4j.appender.DebugAppender.Encoding=ISO-8859-1
log4j.appender.DebugAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.DebugAppender.layout.ConversionPattern=%d{ISO8601} [%t] %-5p %c - %m%n

log4j.appender.ErrorAppender=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.ErrorAppender.rollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.ErrorAppender.rollingPolicy.fileNamePattern=${LogfileRoot}/archived/grails_error_%d{yyyy-MM-dd}.zip
log4j.appender.ErrorAppender.file=${LogfileRoot}/grails_error.log
log4j.appender.ErrorAppender.Threshold=WARN
log4j.appender.ErrorAppender.Encoding=ISO-8859-1
log4j.appender.ErrorAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.ErrorAppender.layout.ConversionPattern=%d{ISO8601} [%t] %-5p %c - %m%n

Enjoy :mrgreen:

Leave a Reply