views:

215

answers:

1

The following is a basic log4j configuration inside Config.groovy using the log4j DSL with Grails 1.2, it works as expected (log all errors to the given file):

log4j = {
    appenders {
        file name:'file', file:"c:/error.log"
    }

    error 'grails.app'

    root {
        error 'file'
    }
}

How would one translate this into a properties style log4j configuration file? The following does not work:

log4j.rootLogger=ERROR, FA

log4j.appender.FA=org.apache.log4j.FileAppender
log4j.appender.FA.File=C:/error.log

log4j.logger.grails.app=ERROR, FA

I suspect it has something to do with the translation of error 'grails.app' but I really don't know. Also, the file doesn't even get created, whereas with the first DSL version, it gets created when the application starts.

If it makes any difference, the properties file is configured externally (However, using an external log4j.groovy file works fine):

grails.config.locations = ["file:${basedir}/extconf/log4j.properties"]

All I really want is an external log4j properties file which logs all application exceptions to a file.

+1  A: 

I believe the problem here is that your properties file is not being used to configure log4j. Including it via grails.config.locations simply won't work.

One option is to put log4j.properties on the classpath, for example directly in 'grails-app/conf'. Check whether that works, get the configuration right, and then check the log4j documentation to find out how you can specify an alternative location for the properties file. It probably involves setting a system property.

Peter Ledbrook
Thanks for the alternative approach. But did something change between Grails 1.1 and Grails 1.2 which invalidates the very clear claim that this can work made in the Definitive Guide (2nd Edition), p.310, Chapter 12 / Externalized Configurations, Listing 12-9?
Stephen Swensen
I'm afraid I don't have access to DGG to check. As far as I know, you have never been able to specify a standard log4j.properties file as an external configuration.You can specify external properties files using `grails.config.locations`, but the Log4j configuration is a rather special case. So with the example you give, you would be able to access the `ConfigurationHolder.config.log4j.rootLogger` property. It just won't have any impact on Log4j itself.That's my understanding, but I may be wrong on this.
Peter Ledbrook