Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring Hibernate logging using Log4j XML config file?

I haven't been able to find any documentation on how to configure Hibernate's logging using the XML style configuration file for Log4j.

Is this even possible or do I have use a properties style configuration file to control Hibernate's logging?

If anyone has any information or links to documentation it would appreciated.

EDIT:
Just to clarify, I am looking for an example of the actual XML syntax to control Hibernate.

EDIT2:
Here is what I have in my XML config file.

<?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="info"/>         <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="1000KB"/>     <!-- Keep one backup file -->         <param name="MaxBackupIndex" value="4"/>         <layout class="org.apache.log4j.PatternLayout">             <param name="ConversionPattern" value="%d [%t] %-5p %l - %m%n"/>         </layout>     </appender>      <root>         <priority value ="debug" />         <appender-ref ref="console" />         <appender-ref ref="rolling-file" />     </root> </log4j:configuration> 

Logging works fine but I am looking for a way to step down and control the hibernate logging in way that separate from my application level logging, as it currently is flooding my logs. I have found examples of using the preference file to do this, I was just wondering how I can do this in a XML file.

like image 236
James McMahon Avatar asked Jan 12 '09 17:01

James McMahon


People also ask

What are the prerequisites to use log4j with Hibernate?

To do logging in Hibernate, you need “slf4j-api. jar” and your preferred binding, like log4j “slf4j-log4j12. jar“. Just declares the dependency in your pom.

Which ways are used by log4j and Logback framework in Hibernate framework to support logging?

Log4j and Logback frameworks can be used in hibernate framework to support logging. There are two ways to perform logging using log4j: By log4j. xml file (or)


2 Answers

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                    Function  org.hibernate.SQL           Log all SQL DML statements as they are executed org.hibernate.type          Log all JDBC parameters org.hibernate.tool.hbm2ddl  Log all SQL DDL statements as they are executed org.hibernate.pretty        Log the state of all entities (max 20 entities) associated with the session at flush time org.hibernate.cache         Log all second-level cache activity org.hibernate.transaction   Log transaction related activity org.hibernate.jdbc          Log all JDBC resource acquisition org.hibernate.hql.ast.AST   Log HQL and SQL ASTs during query parsing org.hibernate.secure        Log all JAAS authorization requests org.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.

like image 101
Loki Avatar answered Oct 06 '22 01:10

Loki


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.

like image 31
Dennis S Avatar answered Oct 06 '22 01:10

Dennis S