Simple Grails Daily Rolling Log Configuration

  • Grails is amazing at many things, but as of v1.1.1 logging configuration and documenting logging configuration is sadly not one of them. The logger has unclear default behavior that must be overridden, and several redundant complex types of syntax that can be used. To make matters worse, it's changed pretty recently and the changes were not forward compatible. This means that Google brings up a lot of useless information.

I had simple logging requirements. For production troubleshooting I want a daily log of everything (even framework code) at the INFO level. The solution was simple, but it took me lots of blind hacking to figure out. Here's what I ended up doing:

First, replace the existing log4j block in grails-app/conf/Config.groovy with this:

log4j = {
    appenders {
        appender new org.apache.log4j.DailyRollingFileAppender(name:"file", fileName:"my_grails_app.log",
                datePattern: '\'_\'yyyy-MM-dd', layout:pattern(conversionPattern: '%d{ISO8601}\t%p\t%c:%L\t%m%n'))
        console name:'stdout', layout:pattern(conversionPattern: '%d{ISO8601}\t%p\t%c:%L\t%m%n')
    }
    root {
        info 'stdout', 'file'
        additivity = true
    }
}

Next, if you don't want lots of logging when you run grails test-app change your environment configuration like this to override the default logging we set up in the previous step:

environments {
    development { ... }
    test {
        ...
        log4j = {
            root {
                error 'stdout'
                additivity = true
            }
        }
        ...
    }
    production { ... }
}

That's really it. You'll now have a daily rolling log where stacktrace.log used to be created and logging will be suppressed in test.