Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using external log4j.properties file with Grails

Tags:

grails

log4j

What's the best way to use an external log4j.properties file within Grails? I'd like to use the traditional log4j.properties format rather than a log4j.groovy style configuration.

I'm also curious if the external configuration will play nicely with the log4j.properties file that's created by grails war and put into the war file. If I remove the log4j configuration from Config.groovy will the log4j.properties still be put into the war file?

like image 653
Rob Hruska Avatar asked Feb 05 '09 22:02

Rob Hruska


2 Answers

In Grails 1 and 2, there's a logging DSL that is configured and used in an out-of-the-box webapp, so you'll want to remove the log4j = { ... } code from grails-app/conf/Config.groovy

If you want to use an external configuration file for logging like in a typical Java web application, then update the file grails-app/conf/spring/resources.groovy with the following.

beans = {
    log4jConfigurer(org.springframework.beans.factory.config.MethodInvokingFactoryBean) {
        targetClass = "org.springframework.util.Log4jConfigurer"
        targetMethod = "initLogging"
        arguments = ["classpath:log4j.properties"]
    }
}

Note that the package name used in your Log4j appender configuration will probably not be what you expect, since it will have a Grails-specific prefix added onto it...

WARN grails.app.controllers.org.example.BookController - This is a warn log message from BookController
ERROR grails.app.controllers.org.example.BookController - This is an error log message from BookController
like image 178
Drew Avatar answered Sep 19 '22 23:09

Drew


If you're using 1.0.x series:

  • copy War.groovy from $GRAILS_HOME/scripts to your $APP/scripts
  • comment out lines 145 and 154 of the copied War.groovy
  • put your log4j.properties into $APP/grails-app/conf
  • run $ grails war, and it will prompt you to choose which War script to run, then choose your local one (usually #1).
like image 31
chanwit Avatar answered Sep 17 '22 23:09

chanwit