Using an external log4j.properties file with Grails

What is the best way to use an external log4j.properties file in Grails? I would like to use the traditional log4j.properties format rather than the log4j.groovy style configuration.

I am also wondering if the external configuration will work well with the log4j.properties file created by grails war and placed in a military file. If I remove the log4j configuration from Config.groovy, will log4j.properties be placed in the war file?

+6
grails log4j
source share
4 answers

In Grails 1 and 2, there is a DSL log that is configured and used in the finished web application, so you 'I need to remove the log4j = { ... } code from grails-app/conf/Config.groovy

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

 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 application configuration will probably not be what you expect, since it will add a special Grails prefix ...

 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 
+6
source share

If you are using 1.0.x series:

  • copy War.groovy from $ GRAILS_HOME / scripts to your $ APP / scripts
  • comment on lines 145 and 154 of the copied War.groovy
  • put your log4j.properties in $ APP / grails-app / conf
  • launch the $ grails war, and it will prompt you to choose which War script to execute, and then select the local one (usually # 1).
+4
source share

Check out the Log4J Plugin , which says:

"Some old-school Java developers are more comfortable with log4j.xml, even if the configuration file is much larger. This plugin provides the ability to disable Log4j's default DSL and allow log4j.xml to be used either in its original form or in the style of Groovy MarkupBuilder."

I did not use it myself, so I can’t talk about its usability in the context of WAR. Just launch your application and then try it. It should be in the WEB-INF folder somewhere obvious. If this does not work, Mingfai on the Grails user list can help you.

+3
source share

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

0
source share

All Articles