如何在Hibernate日志中显示SQL参数?

时间:2013-03-15 05:26:21

标签: sql hibernate logging parameters

在我的hibernate.cfg.xml中,我有以下内容:

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

在我的log4j.xml中,我有以下内容:

<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>

在我的stdout.txt中,我可以看到它记录了SQL语句:

[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=?

但我希望它也能记录SQL的参数,并且它没有显示它。

另一件事是appender被定义为登录到hbn.txt,但SQL语句被记录到stdout.txt而不是hbn.txt。我不知道为什么会这样。

1 个答案:

答案 0 :(得分:11)

在log4j.xml上附加此配置

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

别忘了检查你的appender的Threadhold等级。