Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show SQL parameters in Hibernate log? [duplicate]

In my hibernate.cfg.xml, I have the following:

<property name="show_sql">true</property>

In my log4j.xml, I have the following:

<logger name="org.hibernate" additivity="false">
    <level value="TRACE"/>
    <appender-ref ref="hbn_log"/>
</logger>

<category name="org.hibernate.SQL" additivity="false">
    <priority value="TRACE"/>
    <appender-ref ref="hbn_log"/>
</category>

<category name="org.hibernate.type" additivity="false">
    <priority value="TRACE"/>
    <appender-ref ref="hbn_log"/>
</category>

<logger name="org.hibernate.type.descriptor.sql.BasicBinder"> 
    <level value="TRACE"/>
    <appender-ref ref="hbn_log"/>
</logger>

<logger name="org.hibernate.event.def.DefaultLoadEventListener" additivity="true">
    <level value="all"/>
    <appender-ref ref="hbn_log"/>
</logger>

<logger name="org.hibernate.cache.ReadWriteCache" additivity="true">
   <level value="all"/>
   <appender-ref ref="hbn_log"/>
</logger>

<appender name="hbn_log" class="com.adventnet.management.log.NMSRollingFileAppender">
   <param name="File" value="logs/hbn.txt"/>
       <param name="MaxFileSize" value="1MB"/>
       <param name="MaxBackupIndex" value="10"/>
   <layout class="org.apache.log4j.PatternLayout">
                 <param name="ConversionPattern" value="[%d{dd MMM yyyy HH:mm:ss:SSS}] %-5c{2}: %m%n"/>
   </layout>
   <param name="Threshold" value="TRACE"/>
</appender>

In my stdout.txt, I can see that it logs the SQL statement:

[13 Apr 2013 22:03:59:199] SYS_OUT: Hibernate: select this_.EMSID as EMSID195_0_, this_.COUNTER as COUNTER195_0_, this_.TIMESTAMP as TIMESTAMP195_0_, this_.UPDATETYPE as UPDATETYPE195_0_, this_.OBJECTTYPE as OBJECTTYPE195_0_, this_.OBJECTID as OBJECTID195_0_, this_.OBJECT as OBJECT195_0_ from WebNmsDB.UpdateData this_ where this_.EMSID=?

But I want it to also log the parameter for the SQL, and it's not showing it.

Another thing is that the appender is defined to log to hbn.txt, but the SQL statement gets logged to stdout.txt and not hbn.txt. I don't know why that's happening.

like image 878
pacoverflow Avatar asked Mar 15 '13 05:03

pacoverflow


People also ask

How do you find the origin of a query?

How to find out the origin of the query. The logged query above contains a comment that allows to identify in most cases the origin of the query: if the query is due to a load by ID the comment is /* load your.entity.Name */ , if it's a named query then the comment will contain the name of the query.

What is show_sql property?

show_sql to true tells hibernate to Write all SQL statements to console. This is an alternative to setting the log category org. hibernate. SQL to debug. So even if you set this property to false, make sure that you don't have the following category defined (or configured to use a console appender):


1 Answers

Append this config on your log4j.xml

<logger name="org.hibernate.type.descriptor.sql.BasicBinder"> 
    <level value="TRACE"/>
</logger>

Don't forgot to check your appender's Threadhold level.

like image 168
dgregory Avatar answered Oct 22 '22 10:10

dgregory