Simon Palmer’s blog

Grails

I decided to try out Grails for my latest web project (more to come later). The real advantage is that in an afternoon you can download it, create a few domain objects, set up security, point it at your MySQL instance and have it make a database and a scaffolded app for you. It makes you feel like you are really making progress.

When you come to write a *real* application off the back of it – after all nobody in the real world has apps which resemble the scaffolded pages – things get a bit harder and you have to be fluent in the MVC paradigm and Groovy to get by. Groovy is easy if you know Java, MVC is easily learned and while I think it is a bit purist and unnecessary, I basically agree with separation of concerns, so I’ll swallow that.

Where it all started to get tricky was when I had written a controller which suited my app’s purpose and it suddenly failed to work.

The console window was full of messages – Grails is ridiculously verbose in its messaging – but it all shot past quickly and was only really call stack information on errors. This caused me to look at the logging infrastructure and how to configure it.

I was expecting Log4j, which indeed is part of the standard configuration, but there is no default appender for a file and only errors are reported. It’s a small thing, but you would have thought that a system which boasts “convention-over-configuration” would have a default configuration to create a log file for your app – surely that’s a decent convention. Well, it doesn’t, which I think is an ommission they should correct for the next release (I am using 1.2.0).

That means you have to get into configuring a log file. This has taken me more than a day to figure out, and it is incredibly frustrating because it is not helping me solve my problem, it is me bumping up against the peculiarties of the framework and feel deeply unproductive. If you, dear reader, find yourself in my unfortunate shoes, I am hoping that this will save you that day and the purple language.  (BTW, I have a collection of links at the bottom which were the resources I used to figure this out)

So, here’s what you have to do to get your grails app to log to a file and it is all done in your

/grails-app/conf/config.groovy

This is my first mini-gripe. Why is this a groovy file? This feels like eating your own breakfast unnecessarily. For anyone coming from a Java background who is familiar with configuring Log4j, which is everyone who is starting to use Grails, this is simply an obstruction. Anyway, we’ll leave that gripe aside and get on to the solution.

This is what the default config looks like in my version of Grails.  It is only the log4j section which is relevant for our purposes, so I am leaving out the rest of the file – I haven’t touched it.

// log4j configuration
log4j = {
// Example of changing the log pattern for the default console
// appender:
//
appenders {
    file name:'file', file:'C:/development/grails/main/smyh/smyh.log', append: false
    console name:'stdout'
}
error  'org.codehaus.groovy.grails.web.servlet',  //  controllers
'org.codehaus.groovy.grails.web.pages', //  GSP
'org.codehaus.groovy.grails.web.sitemesh', //  layouts
'org.codehaus.groovy.grails.web.mapping.filter', // URL mapping
'org.codehaus.groovy.grails.web.mapping', // URL mapping
'org.codehaus.groovy.grails.commons', // core / classloading
'org.codehaus.groovy.grails.plugins', // plugins
'org.codehaus.groovy.grails.orm.hibernate', // hibernate integration
'org.springframework',
'org.hibernate',
'net.sf.ehcache.hibernate'

warn   'org.mortbay.log'

info    'org.codehaus.groovy.grails.web.servlet',  //  controllers
'org.codehaus.groovy.grails.web.pages', //  GSP
'org.codehaus.groovy.grails.web.sitemesh', //  layouts
'org.codehaus.groovy.grails.web.mapping.filter', // URL mapping
'org.codehaus.groovy.grails.web.mapping', // URL mapping
'org.codehaus.groovy.grails.commons', // core / classloading
'org.codehaus.groovy.grails.plugins', // plugins
'org.codehaus.groovy.grails.orm.hibernate', // hibernate integration
'org.springframework',
'org.hibernate',
'net.sf.ehcache.hibernate'

debug    'org.hibernate',
'net.sf.ehcache.hibernate'

root {
// change the root logger to my tomcatLog file
error 'file', stdout
info 'file', stdout
warn 'file', stdout
debug 'file', stdout
additivity = true
}

Useful Links:

The grails documentation (please!)
Stackoverflow question log4j-in-grails-how-to-log-into-file (give it an up-vote)
Stackoverflow question grails-1-1-and-how-to-info-level-logging
The Log4j documentation

4 Comments »

  1. Thank you Simon. You saved me a lot of pain and frustration. Many thanks for sharing.

    Comment by Hani — May 3, 2010 @ 1:50 pm

  2. Thanks Simon, It really helped!!!

    Comment by Puja — June 25, 2011 @ 10:03 am

  3. Thank you! After 3 days of being mocked, I finally came to the right place to find the answer.

    Comment by Bill — August 10, 2011 @ 2:03 am

  4. Hi Simon,

    Thank you this was really helpful.

    You had long time back posted a question in stackoverflow on the issue of Primary Key IDs are showing up as comma separated when larger than 999 – ” I can’t seem to get an Integer value as a genuine integer into my code if it is greater than 999 because of the default (and unconfigurable) behaviour of the fieldValue directive.”.

    Were you able to find any default configuration setting that would resolve this problem? I could remove the comma from string in the controller, but am trying to find a easier and configurable workaround such that I don’t have to go change it at every other GSP or controller

    Any insight you can provide will be helpful.

    Thanks

    Comment by Ashish — October 21, 2011 @ 10:01 am


RSS feed for comments on this post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: