How to use an external log4j.properties in you Grails project
In a recent Grails project, I had to follow some corporate guidelines regarding application deployment, and one of those were that the log4j configuration for the application must be externalized and in the properties format.
I searched for a Grails plugin that could help me on this, with no luck. Then I remembered that a Grails application is just a Spring application in disguise, so I looked for the Spring way to do this.
There are at least two ways to do this using Spring: Log4jConfigListener and Log4jConfigurer. I choose the later because the former assumes an expanded WAR file, which was not my case.
Here’s the recipe I came up with:
- Configure a Log4jConfigurer Spring bean in your
grails-app/conf/resources.groovy: (see here for a resources.xml version)
Install the templates in your project with
grails install-templates, so you can change some files used for Grails' code generation. The one we are interested is theweb.xmltemplateComment out the Grails'
Log4jConfigListenerfrom thesrc/templates/war/web.xmltemplate:
You can (and should) remove the log4j configuration block from your Config.groovy
That’s it!
This was tested with Grails 1.3.3, deploying to an Oracle WebLogic 10.3.0 container.


Comments 7 Comments
I have been looking for such solution for grails for so long.
Prior to this i thought it can only be configured in Config.groovy only ,though grails have simplified the conf. yet it always need to recompiled in order to make changes for logger levels.
Cheers:)
Using grails without custom beans so far. How do I write this XML in Spring DSL?
Thanks
I updated the post, using the resources.groovy to declare the Log4JConfigurer bean.
Anyway, if you want to know more about the syntax of the Spring context files, take a look at some exemples and docs here: http://static.springsource.org/spring/docs/3.0.x/reference/beans.html#beans-f...