Configure Hibernate Logging Using the Log4j XML Configuration File?

I could not find the documentation on how to configure Hibernate logging using the XML style configuration file for Log4j.

Is this possible, or am I using a style-style configuration file to control the Hibernate protocol?

If anyone has any information or links to documentation, this will be appreciated.

EDIT:
To clarify, I'm looking for an example of the actual XML syntax for managing Hibernate.

EDIT2:
Here is what I have in the XML configuration 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'm looking for a way to resign and control hibernation logging so that it is separate from logging at the application level, as it is currently flooding my logs. I found examples of using a preference file for this, I'm just wondering how I can do this in an XML file.

+80
xml logging hibernate configuration log4j
Jan 12 '09 at 17:40
source share
6 answers

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

Here is a list of registrar 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 to be inserted into the 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" /> 

Note. Most registrars use the DEBUG level, however org.hibernate.type uses TRACE. In previous versions of Hibernate, org.hibernate.type also used DEBUG, but with Hibernate 3 you must set the level to TRACE (or ALL) to view the JDBC parameter binding record.

And the category is defined as such:

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

It should be placed in front of the root element.

+142
Jan 12 '09 at 19:32
source share

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

A lot of beating, waving and common dead mice ran through, finally brought me my cheese.

Since Hibernate 3 uses the Simple Logging Phase for Java (SLF4J) (in the docs), if you rely on Log4j 1.2 you will need and need slf4j-log4j12-1.5.10.jar if you want to fully configure Hibernate logging with configuration log4j file. Hope this helps the next guy.

+25
Feb 25 '10 at 17:06
source share

In response to a homaxto 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> 

Key part

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

Hope this helps.

+7
Feb 06 '09 at 15:56
source share

Here is what I use:

 <logger name="org.hibernate"> <level value="warn"/> </logger> <logger name="org.hibernate.SQL"> <level value="warn"/> </logger> <logger name="org.hibernate.type"> <level value="warn"/> </logger> <root> <priority value="info"/> <appender-ref ref="C1"/> </root> 

Obviously, I don't like the Hibernate posts;) - set the level to "debug" to get the result.

+5
Mar 12 '09 at 16:19
source share

The answers were helpful. After this change, I got duplicate SQL query maintenance, one in the log4j log file and one in the standard console. I changed the persistence.xml file to tell show_sql to false to get rid of logging from the standard console. Keeping format_sql true also affects the log4j log file, so I saved that value.

 <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> <persistence-unit name="myUnit" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <properties> <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/> <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:file:d:\temp\database\cap1000;shutdown=true"></property> <property name="dialect" value="org.hibernate.dialect.HSQLDialect"/> <property name="hibernate.show_sql" value="false"/> <property name="hibernate.format_sql" value="true"/> <property name="hibernate.connection.username" value="sa"/> <property name="hibernate.hbm2ddl.auto" value="create-drop"/> </properties> </persistence-unit> </persistence> 
+3
Jul 25 2018-12-18T00:
source share

You can configure your log4j file with a category tag (see console example):

 <appender name="console" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yy-MM-dd HH:mm:ss} %p %c - %m%n" /> </layout> </appender> <category name="org.hibernate"> <priority value="WARN" /> </category> <root> <priority value="INFO" /> <appender-ref ref="console" /> </root> 

Thus, each warning, error message or fatal message from sleep mode will be displayed, nothing more. In addition, the code of the code and the library will be at the information level (so that information, warning, error and fatal)

To change the log level in the library, simply add a category, for example, to the deactivation spring info log:

 <category name="org.springframework"> <priority value="WARN" /> </category> 

Or with another addition, break additivity (the default value for additivity is true)

 <category name="org.springframework" additivity="false"> <priority value="WARN" /> <appender-ref ref="anotherAppender" /> </category> 

And if you do not want this hibernate to register every request, set the show_sql hibernate show_sql to false .

0
Oct 08 '13 at 14:07
source share



All Articles