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.

Comments
Why not use servlet container's native logging?
I think, for production environments, it may be worthwhile to have all logging configuration be done by the servlet container itself (i.e. its admins). Grails can be configured to step-aside and let the servlet container take over. See http://blog.saddey.net/2010/02/07/grails-how-to-use-native-server-loggin...
Workaround for this problem
For a workaround to the problem, you can use if statements in the log4j configuration dsl
log4j = {appenders {
def logfileName = 'mylog.log'
if(Environment.current == Environment.PRODUCTION) {
logfileName = 'my_production.log'
}
rollingFile name: "mylog", file: logfileName, layout:pattern(conversionPattern: '%d{ISO8601} [%t] %p %c %x - %m%n')
}
if(Environment.current == Environment.PRODUCTION) {
root { info "mylog" }
} else {
root { debug "mylog" }
}
}
Which Grails version
The Grails documentation for version 1.2.x says This single Config.groovy file allows you to specify separate logging configurations for development, test, and production environments.
In which version did you check that it didn't work?