Ancient Programming

What I encounter in my software part of life is in danger of being commented upon here

Logging sql statements and parameters using hibernate

Posted by Jacob von Eyben on November 12th, 2009

If you need to log sql statements using hibernate you can turn debug on for the org.hibernate.sql logger.
That will log all sql statement, but the actual parameter values will not be logged.

To log the values bound to the hibernate prepared statements, you can turn on trace for the org.hibernate.type logger.

Unfortunately (apparently because of perfomance) the value of the org.hibernate.type logger is evaulated once in a static block, and cached, see:

That prevents you from turning the log on and off during runtime using a simple jsp page like the log4jAdmin.jsp. Note that if it were possible you still had to add trace to the page as debug is currently the lowest value.

Of course there must be a reason why the hibernate implementation are caching the value of the org.hibernate.type logger, but I believe it is still valuable to be able to turn on sql logging with parameters at runtime.

For that purpose I came across the logDriver. A simple database driver written by Ryan Bloom, that is wrapping an existing jdbc driver.

To use the logdriver all you have to do is set the driver and connection url as follows:


Now you can turn on logging by setting the net.rkbloom.logdriver logger to debug.

One Response to “Logging sql statements and parameters using hibernate”

  1. Morten Holm Says:

    log4jdbc ( is another candidate for the job. Same thing. Change the driver - prepend the entire jdbc url with ‘jdbc:log4′ and you are running.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>