Grails Hibernate Logging

As a little bit of a Hibernate veteran at this point I know the value of logging when it comes to debugging issues. So before I even get my first application going here I figured I would setup hibernate logging. I am not going to go through everything you can do with logging, you can check out the docs for that.

Enabling Hibernate logging is actually really simple. Open up your datasource configuration file and add logsql to the environment you wish to enable logging in. You can add it to the hibernate settings but its a safe bet to just add it to your development environment. I also added the format_sql setting which gives you some nice formatting of the generated SQL.

Next we need to add a couple of lines to the log4j settings. Open up your Config.groovy file and go down to the log4j section. We need to add a debug line for ‘org.hibernate.SQL’. This is actually all we need to do to log statements. Sometimes though its nice to see what parameters are being binding to queries. To see these we need that trace statement. Make sure you get specific here, if you just log ‘org.hibernate.type’ you will get a lot of info that you really don’t care to have.

That is all there is to it folks, pretty easy stuff. Below is a console view of an insert into the user table followed by a select. We can see what values are being inserted and the nice thing is it shows each type. Notice the nice formatting for the SQL as well.

Hi, I’m Dan Vega. I am a Software Engineer living just outside of one of my favorite places in the world, Cleveland, Ohio. I am a self-taught programmer who firmly believes that you can accomplish anything in this life if you put your mind to it, roll up your sleeves and are willing to put in the work.

I have been writing software for over 18+ years now and I use my knowledge, passion, and influence to help other developers achieve their goals. I teach online and currently, have over 30,000 students.

When I am not writing software, I have a number of things that really keep me busy. I love to read books, listen to podcasts and watch online courses. I have a real thirst for knowledge and it consumes me every single day of the week. I also love to run & lift heavy weights which keep me balanced throughout the week.

Please note: I reserve the right to delete comments that are offensive or off-topic.