Logging a Spring Hibernate application -SLF4J logging with Log4J and JCL

Posted: May 16, 2012 in Techilla
Tags: , , , ,

Logging a Spring +Hibernate application is especially useful during the initial stages when you would want to see whether all Hibernate configurations are fine. It might also be useful at the later stages to look at the query log generated to do some optimization. However, logging of combined Spring and Hibernate related data onto the same log file is not very straightforward (until you read this post, that is :). Its primarily because Spring and Hibernate use different logging technologies

Spring uses Jakarta Commons Logging API (JCL) which is mandatory to be in the classpath or the application context doesn’t get loaded.

Hibernate 3.3+ uses SL4J or Simple Logging Façade for Java

I have always used log4j to have a common logging repository for our application,

Now, for all these to work together and post everything onto the log file, we need the following libraries in our classpath:

  • commons-logging-1.1.1.jar
  • log4j-1.2.13.jar
  • slf4j-api-1.6.1.jar
  • slf4j-log4j12-1.6.4.jar

The last jar is the bridge between SL4J and log4j, however note that sl4j-log4j* and sl4j-jdk* cannot remain on the classpath at the same time, or they cause binding issues, so look out for that.(Will print on the logs if you have any such binding issues)

Finally, our log4j.xml which will contain the logging info.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
	debug="false">

	<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %5p %c{2}: %m%n" />
		</layout>
	</appender>

	<appender name="ASYNC" class="org.apache.log4j.AsyncAppender">
		<appender-ref ref="CONSOLE" />
		<appender-ref ref="FILE" />
	</appender>

	<appender name="FILE" class="org.apache.log4j.RollingFileAppender">

		<param name="File" value="C:/log/spring-hib.log" />
		<param name="MaxBackupIndex" value="100" />

		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %5p %c{2}: %m%n" />
		</layout>

	</appender>

	<category name="org.hibernate">
		<priority value="DEBUG" />
	</category>

	<category name="java.sql">
		<priority value="debug" />
	</category>

	<root>
		<priority value="INFO" />
		<appender-ref ref="FILE" />
	</root>

</log4j:configuration>

Note : org.hibernate will show the full blown list of boiler plate stuff , so it might be worthwhile to trim down to org.hibernate.SQL.

Also, if u put hibernate.show_sql=true in your configuration file, it will print the sql statement in the console as well.

There is a very nice pictorial representation here which shows the various binding among different loggers and it pointed me in the right direction, thanks Espen!:)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s