Written on
Grails - Multiple Log4j Configurations
This is a quick tip that I found very useful and - to my knowledge - is not part of the Grails documentation by now. So let's spread the word :-) In one of my Grails projects, we use external configuration files to apply customer specific settings. We have a custom ConfigurationLoader that is used to locate customer-specific configuration files based on the current environment (VM arguments, *.properties file and such). The configuration loader returns a list of additional configuration files which is handed over to grails.config.locations [0]
// ConfigurationLoader retrieves customConfigLocations, being an ArrayList<String>
grails.config.locations = customConfigLocations
// log4j configuration
log4j = {
error 'org.codehaus.groovy.grails.web.servlet', // controllers
'org.codehaus.groovy.grails.web.pages', // GSP
'org.codehaus.groovy.grails.web.sitemesh', // layouts
'org.codehaus.groovy.grails.web.mapping.filter', // URL mapping
'org.codehaus.groovy.grails.web.mapping', // URL mapping
'org.codehaus.groovy.grails.commons', // core / classloading
'org.codehaus.groovy.grails.plugins', // plugins
'org.codehaus.groovy.grails.orm.hibernate', // hibernate integration
'org.springframework',
'org.hibernate',
'net.sf.ehcache.hibernate'
}
Using a map of closures
As of Grails 2.0, there is a way to extend the Log4J configuration rather than replacing it. The way to go is to use a log4j variable of type Map<String, Closure> instead of Closure.
// main configuration
log4j.main = {
// ...
}
// external (customer-specific) configuration
log4j.external = {
// common configuration
}
environments {
development {
log4j.env = {
// environment-specific config
}
}
production {
log4j.env = {
// environment-specific config
}
}
}
[2] Jira GRAILS-7314