I have no special talent. I'm only passionately curious - Albert Einstein
Grails Logging Gotchas Comment on Grails Logging Gotchas 0

One of the most frustrating experiences for me with Grails so far has been with what should be extremely simple; logging.  There are countless blog entries on the topic, but few are thorough in covering multiple aspects of logging.  The Grails documentation only seems to provide adequate documentation for one aspect - configuring the root logger, and appenders.  From there, you're left with quite a few more questions.  Since I've been moving full-steam ahead in my Grails development efforts, I figured I'd share some of my findings about logging.

When working with the dynamically injected loggers in Grails, you are typically only concerned with about three things - the root logger, appenders, and closures for each logging level.  Grails has excellent support for environment specific logging levels, but they seemed to have dropped the ball on adding native environment support for the logging levels.  So far, my favorite way to acheive this task is using the good ol' Environment util to wrap each log level closure:

 

//wrap the debug closure with an Environment guard clause
//this will set the grails.app bootstrap, controller, and service loggers
//to 'debug' only in the development environment
if(Environment.current == Environment.DEVELOPMENT) {   
        debug 'grails.app.bootstrap',
              'grails.app.controller',
              'grails.app.service'   
}

 

 

Another extremely frustrating discovery I've noticed is the inconsistency which loggers are injected.  I want to be able to use my log4j configurations within my integration tests.  No such luck, however, since Grails doesn't inject log4j loggers into Integration tests.  Another funky dependency injection issue I discovered was in using loggers in afterPropertiesSet in classes that implement Spring's InitializingBean interface.  You can't use a logger there, either due to some bug in Grails related to dependency injection.

 

My last beef with Grails is the inconsistency with String objects and placeholders.  Grails documentation leads you to believe that you can initialize a String object with either single or double quotes.  But Grails doesn't treat all strings alike.  Log statements must be wrapped in double quotes if you're going to use dynamic placeholders for objects to include in your logging statements.  This is probably the most frustrating little tidbit to deal with especially since Grails is supposed to be treating all String objects alike, both with single quotes and double quotes.

//it doesn't matter if this String object is wrapped in single quotes OR double quotes
def test = "now you don't"
log.debug 'now you see me, $test'
log.debug "now you see me, $test" <- placeholder is replaced as expected

Same is true with println statements:

println 'now you see me, $test'
println "now you see me, $test" <- placeholder is replaced as expected

 

So, when working with Grails be aware of these pesky gotchas.


0 comments

Comments are currently disabled

About

David Malone is a Java developer residing in the Twin Cities area.  He has been developing enterprise applications since 2004.  This is his personal blog, as well as his design and development workspace.