Configuring Hibernate logging using Log4j XML config file? Configuring Hibernate logging using Log4j XML config file? xml xml

Configuring Hibernate logging using Log4j XML config file?


From http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html#configuration-logging

Here's the list of logger categories:

Category                    Functionorg.hibernate.SQL           Log all SQL DML statements as they are executedorg.hibernate.type          Log all JDBC parametersorg.hibernate.tool.hbm2ddl  Log all SQL DDL statements as they are executedorg.hibernate.pretty        Log the state of all entities (max 20 entities) associated with the session at flush timeorg.hibernate.cache         Log all second-level cache activityorg.hibernate.transaction   Log transaction related activityorg.hibernate.jdbc          Log all JDBC resource acquisitionorg.hibernate.hql.ast.AST   Log HQL and SQL ASTs during query parsingorg.hibernate.secure        Log all JAAS authorization requestsorg.hibernate               Log everything (a lot of information, but very useful for troubleshooting) 

Formatted for pasting into a log4j XML configuration file:

<!-- Log all SQL DML statements as they are executed --><Logger name="org.hibernate.SQL" level="debug" /><!-- Log all JDBC parameters --><Logger name="org.hibernate.type" level="debug" /><!-- Log all SQL DDL statements as they are executed --><Logger name="org.hibernate.tool.hbm2ddl" level="debug" /><!-- Log the state of all entities (max 20 entities) associated with the session at flush time --><Logger name="org.hibernate.pretty" level="debug" /><!-- Log all second-level cache activity --><Logger name="org.hibernate.cache" level="debug" /><!-- Log transaction related activity --><Logger name="org.hibernate.transaction" level="debug" /><!-- Log all JDBC resource acquisition --><Logger name="org.hibernate.jdbc" level="debug" /><!-- Log HQL and SQL ASTs during query parsing --><Logger name="org.hibernate.hql.ast.AST" level="debug" /><!-- Log all JAAS authorization requests --><Logger name="org.hibernate.secure" level="debug" /><!-- Log everything (a lot of information, but very useful for troubleshooting) --><Logger name="org.hibernate" level="debug" />

NB: Most of the loggers use the DEBUG level, however org.hibernate.type uses TRACE. In previous versions of Hibernate org.hibernate.type also used DEBUG, but as of Hibernate 3 you must set the level to TRACE (or ALL) in order to see the JDBC parameter binding logging.

And a category is specified as such:

<logger name="org.hibernate">    <level value="ALL" />    <appender-ref ref="FILE"/></logger>

It must be placed before the root element.


Loki's answer points to the Hibernate 3 docs and provides good information, but I was still not getting the results I expected.

Much thrashing, waving of arms and general dead mouse runs finally landed me my cheese.

Because Hibernate 3 is using Simple Logging Facade for Java (SLF4J) (per the docs), if you are relying on Log4j 1.2 you will also need the slf4j-log4j12-1.5.10.jar if you are wanting to fully configure Hibernate logging with a log4j configuration file. Hope this helps the next guy.


In response to homaxto's comment, this is what I have right now.

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"><log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">    <appender name="console" class="org.apache.log4j.ConsoleAppender">        <param name="Threshold" value="debug"/>        <param name="Target" value="System.out"/>        <layout class="org.apache.log4j.PatternLayout">            <param name="ConversionPattern" value="%d{ABSOLUTE} [%t] %-5p %c{1} - %m%n"/>        </layout>    </appender>    <appender name="rolling-file" class="org.apache.log4j.RollingFileAppender">        <param name="file" value="Program-Name.log"/>        <param name="MaxFileSize" value="500KB"/>        <param name="MaxBackupIndex" value="4"/>        <layout class="org.apache.log4j.PatternLayout">            <param name="ConversionPattern" value="%d [%t] %-5p %l - %m%n"/>        </layout>    </appender>    <logger name="org.hibernate">        <level value="info" />    </logger>    <root>        <priority value ="debug" />        <appender-ref ref="console" />        <appender-ref ref="rolling-file" />    </root></log4j:configuration>

The key part being

<logger name="org.hibernate">    <level value="info" /></logger>

Hope this helps.