8

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.

pacoverflow
  • 3,726
  • 11
  • 41
  • 71

1 Answers1

13

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.

dgregory
  • 1,397
  • 1
  • 12
  • 26
  • I added that to my log4j.xml and still don't get any parameter logging. – pacoverflow Mar 15 '13 at 05:40
  • I also added to the appender and it still doesn't work. – pacoverflow Mar 15 '13 at 05:43
  • This works perfectly for me: – al. Jul 29 '15 at 07:49
  • You may need to tell Hibernate what logging manager you use (log4j, slf4j), see [how](http://stackoverflow.com/questions/11639997/how-do-you-configure-logging-in-hibernate-4-to-use-slf4j). – Vlastimil Ovčáčík Aug 09 '15 at 18:35
  • You can set the following parameters for log4j "log4j.logger.org.hibernate.SQL=debug","log4j.logger.org.hibernate.type.descriptor.sql=trace" Please visit this link for more details :- http://techpost360.blogspot.in/2016/10/hibernate-show-sql-with-parameter-values.html – Rahul Wagh Oct 10 '16 at 11:09