Wednesday, November 12, 2008

Grails Logging

Configuring logging in Grails is not as simple one would think. Grails uses Log4J as its logging framework. Log4J configuration is stored in the log4j.properties file. In Grails, this file is generated from Config.groovy source file.

The official Grails Logging documentation says that if you want to have a different logging levels for let's say a specific controller, all controllers and the rest of your application, you should define the logging levels as:

grails.'app.controller.YourOtherController'="off,stdout"
grails.'app.controller'="info,stdout"
grails.app="error,stdout"

Yes, those ticks are required, otherwise you get an error message (No such property: context for class: java.lang.String). Don't ask me why. What is also important is the order. Children need to go first.

So that is what the official documentation and few blogs say.

Grails allows you to configure your production, development and testing environments differently if required. I configured Log4J the way that common properties are grouped and placed outside the environment dependent ones. But no matter how I tweaked it, I could not make it to work. For some reason the parent's or child's definition overrides the other, or causes an error.

But there is a solution to this problem after all. Instead of dot notation, use curly braces. Like in the following example:

environments {
development {
...
log4j {
appender.stdout = ...
rootLogger="error,logfile,stdout"
logger {
grails {
app {
controller="debug"
}
}
}
}
}
production {
...
}
}

// log4j configuration
log4j {
appender.logfile=...
appender.stacktraceLog=...
rootLogger="debug,logfile"
logger {
grails="error"
StackTrace="error,stacktraceLog"
...
}
additivity.StackTrace=false
}

This works. Tested with Grails 1.0.3.

Related articles:

1 comment:

Ben said...

Very tasty.


Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License.