30 Sep 2010

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 the web.xml template

  • Comment out the Grails' Log4jConfigListener from the src/templates/war/web.xml template:

  • 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.