• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

To log sql in Hibernate

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are 2 options in current Hibernate regarding loggin sql:
1, show-sql=true this will output the generated sql to system console (if you look at the source, System.out.println() is used)
2, or in the log4j configuration, set "org.hibernate.sql" logging level DEBUG, this way, Hibernate will log to log file, however it will log lots of hibernate activity along with the sql.

Sometime I need to log those generated sql to file, and I want the logged sql to be "real"(or at least close) with real parameters instead of "?" of prepared statement.

What I did was to download the Hibernate source, unzip, add a new wrapper class "LoggablePreparedStatement", which implements the PreparedStatement and also have function to "record" the parameters, I then modified one Hibernate class "AbstractBatcher" to use the new loggablePreparedStatement instead of the regular PreparedStatement.

After this, re-build Hibernate and put it in the classpath. In the log4j, add
<logger name="org.hibernate.jdbc.LoggablePreparedStatement">
<level value="debug"/>
<appender-ref ref="sqlFile"/>
</logger>

This way, I can have better looking sql logged to file and this helps me in debugging.

I hope this will help those who might have same needs (I wish Hibernate development team could do something to improve the sql logging)

Notes: I got the LoggablePreparedStatement several years ago from an IBM article, which I cound not find now.
 
Jerry You
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know how to attach file here. Maybe will post the code (it is very long) later.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
or check out p6spy.com

Mark
 
Jerry You
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indeed - even better.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic