Grails hip tip: logging SQL

how-to
Jun 8, 20091 min

When working with Grails (especially, when slapping it over legacy database, man) it’s often helpful to see the hip SQL the framework generates when manipulating domain objects. As mapping a legacy database to Grails can be tricky (but indeed, solvable, baby!), seeing generated SQL can unveil incorrect mapping, etc.

Luckily, you can easily enable SQL logging without having to mess with Log4J configurations (i.e. debug, error, warn “packages blah blah”). In the DataSource.groovy file in your project’s grails-app/conf directory, simply add the loggingSql variable to a desired dataSource closure (that is, you can enable this feature for a particular environment or all of them) like so:

<pre class="prettyprint"><code>dataSource {
  //blah blah
  loggingSql = true
}

After that, you’ll notice that all SQL statements Grails (or really, Hibernate) utilizes will be logged to whichever appender you’ve configured (i.e. System.out, by default). This little tip has proved quite copacetic for me, man! Can you dig it?

You can now follow The Disco Blog on Twitter, baby!
andrew_glover

When Andrew Glover isn't listening to “Funkytown” or “Le Freak” he enjoys speaking on the No Fluff Just Stuff Tour. He also writes articles for multiple online publications including IBM's developerWorks and O'Reilly’s ONJava and ONLamp portals. Andrew is also the co-author of Java Testing Patterns, which was published by Wiley in September 2004; Addison-Wesley’s Continuous Integration; and Manning’s Groovy in Action.

More from this author