Thursday, September 26, 2013

Enabling log4j logging in Hibernate

Hibernate 3 has integrated with slf4j logging which demands an additional step in getting Hibernate’s log4j logs. This is important because in debug mode, Hibernate logs the SQL query strings and the bind variable values. The steps to configure for capturing the log4j logs from Hibernate are below:
1. Place log4j, slf4j-api and slf4j-log4j jars in the classpath. I am using log4j-1.2.15.jar, slf4j-api-1.5.3.jar and slf4j-log4j12-1.5.3.jarfiles.
2. Now configure the log4j.properties (or XML) file and place it in your classpath. A sample properties file content is provided below. This will print all the logs on to the console. Configure a file appender if you want the logs to be writtwen to a file.
Sample log4j.properties:

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.rootLogger=debug, stdout
log4j.logger.org.hibernate=debug
log4j.logger.org.springframework=debug
log4j.logger.org.hibernate.hql.ast.AST=info
log4j.logger.org.hibernate.SQL=trace
log4j.logger.org.hibernate.type= trace
log4j.logger.org.hibernate.tool.hbm2ddl=warn
log4j.logger.org.hibernate.hql=debug
log4j.logger.org.hibernate.cache=info
log4j.logger.org.hibernate.jdbc=debug

You are now good to go :) Happy logging !!

No comments:

Post a Comment