Quick Grails Logging Tip

Posted: November 28th, 2011 | Author: | Filed under: Groovy/Grails | No Comments »

My fun with Grails continues. Today I was having a bit of difficulty wrapping my head around the concept of dumping and logging. Dumping variables to the screen or to a log file is easy in ColdFusion thanks to the CFDUMP and CFLOG tags.

One way to dump variables in Grails is to output them to the console. After reading through this and asking a co-worker for assistance I still had to tinker a bit before I could get log.trace() statements from displaying in the console.

So as it turns out, I had to modify the log output level for an entity in Grails modify the log4j code block in your application’s Config.groovy file.

trace 'grails.app.controller.chapter4.SongController'

In this example “grails.app.controller” refers to ‘one or more controllers’ and ‘chapter4.SongController’ is the classpath and controller name. The code basically says ‘display all log messages at or above the trace level in SongController’. You can read more about logging levels here.

Here is a slight modification to the above code. This code allowed me to change the log settings for multiple controllers.

trace 'grails.app.controller.chapter4.SongController', 
'grails.app.controller.chapter4.AlbumController'

Note that you can apply the same log settings to all controller files…

Example C: trace 'grails.app.controller'

And for reference here is SongController.groovy…

package chapter4
class SongController {
 
	def index = {
		log.trace("I can see log.trace")
		log.warn("I can see log.warn")
		log.error("I can see log.error")
	}
}

OK, back to the code. I’m deep into Chapter 4 of The Definitive Guide to Grails.



Leave a Reply