log4j

Steve Dalton's picture

Grails log4j configuration and custom environments

If like me, you have different appenders depending on the grails environment - you might not have realised that custom environments don't work with the log4j config DSL. So code like this doesn't work:

log4j = {
     appenders {
         development {
             rollingFile name: "mylog",
                     file: 'mylog.log', layout: pattern(conversionPattern: '%d{ISO
         }
         test {
             rollingFile name: "mylog",
                     file: 'mylog.log', layout: pattern(conversionPattern: '%d{ISO
         }
         beta {
             rollingFile name: "mylog",
                     file: '/tmp/mylog-beta.log',
                     layout: pattern(conversionPattern: '%d{ISO8601} [%t] %p %c
         }
         production {
            rollingFile name: "mylog",
                     file: '/tmp/mylog-beta.log',
                     layout: pattern(conversionPattern: '%d{ISO8601} [%t] %p %c
         }
    }

The beta environment won't be picked up and you will get some error in your logs about it for other environments. In any case the configuration doesn't look very DRY to me. Probably better to just have the filename into a separate config param in your Config.groovy, then just refer to it from a single appender

myapp.logLocation = 'mylog.log'

environments {
    production {
        myapp.logLocation = '/tmp/mylog.log'
    }
    beta {
        myapp.logLocation = '/tmp/mylog.log'
    }
}

log4j = {
    appenders {
            rollingFile name: "mylog",
                    file: myapp.logLocation, layout: pattern(conversionPattern: '%d{ISO8601} [%t] %p %c %x - %m%n')
    }

Still not completely very DRY, wish I could do something like

environments {
     production, beta {
         somesharedconfig = 'foo'
     }

Hopefully they'll come up with some way to do this nicely in future grails releases.

Syndicate content