Rob Hruska
Rob Hruska

Reputation: 120286

Using external log4j.properties file with Grails

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?

Upvotes: 6

Views: 8134

Answers (4)

Drew
Drew

Reputation: 1937

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

Upvotes: 6

Juanjo García
Juanjo García

Reputation: 1

At least in 1.0.x series (not checked in 1.1) forget about Grails support and rely directly on Spring. Just use the log4jConfigLocation parameter in web.xml. More info here

Upvotes: 0

chanwit
chanwit

Reputation: 3214

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

Upvotes: 4

j pimmel
j pimmel

Reputation: 11637

Check out the Log4J Plugin which states:

"Some old-school Java developers are more comfortable with log4j.xml even though the configuration file is much larger. This plugin provides a way to disable the default Log4j DSL and allow the use of log4j.xml either in the original form, or in Groovy MarkupBuilder style."

I haven't used it myself so can't speak to its usability in the WAR context.. Just WAR up your app, and then try it out.. It should be under the WEB-INF folder somewhere obvious. If that doesn't work, Mingfai on the grails user list can probably help you out.

Upvotes: 3

Related Questions