Main Tutorials

How to configure logging in Hibernate – Logback

In this tutorial, we show how to integrate Logback logging framework with Hibernate.

Tools and Technologies used in this tutorial :

  1. Hibernate 3.6.3.Final
  2. slf4j-api-1.6.1
  3. logback-core-0.9.28
  4. logback-classic-0.9.28
  5. Eclipse 3.6
  6. Maven 3.0.3

1. Get SLF4j + Logback

To use logback in Hibernate web application, you need 3 libraries :

  1. slf4j-api.jar
  2. logback-core
  3. logback-classic

File : pom.xml


<project ...>
	<repositories>
		<repository>
			<id>JBoss repository</id>
			<url>http://repository.jboss.org/nexus/content/groups/public/</url>
		</repository>
	</repositories>

	<dependencies>

		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>3.6.3.Final</version>
		</dependency>

		<!-- logback -->
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-core</artifactId>
			<version>0.9.28</version>
		</dependency>

		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>0.9.28</version>
		</dependency>

	</dependencies>
</project>
Where is slf4j-api.jar?
The slf4j-api.jar is defined as the dependency of “hibernate-core“, so , you do not need to declare it again.

2. logback.xml

Create a “logback.xml” file and put it into your project’s classpath, see figure below :

logback hibernate

A classic “logback.xml” example.


<?xml version="1.0" encoding="UTF-8"?>
<configuration>

 <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
	<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
		<Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} %-5level %logger{36} - %msg%n
                </Pattern>
	</encoder>
 </appender>

 <appender name="FILE"
	class="ch.qos.logback.core.rolling.RollingFileAppender">
	<file>c:/mkyongapp.log</file>
	<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
	   <Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
           </Pattern>
	</encoder>

	<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
		<FileNamePattern>c:/mkyongapp.%i.log.zip</FileNamePattern>
		<MinIndex>1</MinIndex>
		<MaxIndex>10</MaxIndex>
	</rollingPolicy>

	<triggeringPolicy
		class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
		<MaxFileSize>2MB</MaxFileSize>
	</triggeringPolicy>

  </appender>

  <logger name="org.hibernate.type" level="ALL" />
  <logger name="org.hibernate" level="DEBUG" />

  <root level="INFO">
	<appender-ref ref="FILE" />
	<appender-ref ref="STDOUT" />
  </root>

</configuration>

With this logback.xml configuration, it means, redirect all your web application logging outputs to console and also a file at “c:/mkyongapp.log“.

3. Output

See logback logging output at “C:\\mkyongapp.log” below :


//...
2011-04-23_14:34:08.055 [main] DEBUG o.h.transaction.JDBCTransaction - commit
2011-04-23_14:34:08.056 [main] DEBUG o.h.e.d.AbstractFlushingEventListener - processing flush-time cascades
2011-04-23_14:34:08.056 [main] DEBUG o.h.e.d.AbstractFlushingEventListener - dirty checking collections
2011-04-23_14:34:08.058 [main] DEBUG o.h.e.d.AbstractFlushingEventListener - Flushed: 1 insertions, 0 updates, 0 deletions to 1 objects
2011-04-23_14:34:08.058 [main] DEBUG o.h.e.d.AbstractFlushingEventListener - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
2011-04-23_14:34:08.059 [main] DEBUG org.hibernate.pretty.Printer - listing entities:
2011-04-23_14:34:08.060 [main] DEBUG org.hibernate.pretty.Printer - com.mkyong.user.DBUser{username=Hibernate101, createdBy=system, userId=100, createdDate=Sat Apr 23 14:34:08 SGT 2011}
2011-04-23_14:34:08.064 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
2011-04-23_14:34:08.066 [main] DEBUG org.hibernate.SQL - insert into MKYONG.DBUSER (CREATED_BY, CREATED_DATE, USERNAME, USER_ID) values (?, ?, ?, ?)
2011-04-23_14:34:08.150 [main] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - system
2011-04-23_14:34:08.152 [main] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [2] as [DATE] - Sat Apr 23 14:34:08 SGT 2011
2011-04-23_14:34:08.153 [main] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [3] as [VARCHAR] - Hibernate101
2011-04-23_14:34:08.153 [main] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [4] as [INTEGER] - 100
Download it – Logback-Hibernate-Example.zip (8KB)

Reference

  1. Simple Logging Facade for Java (SLF4J)
  2. Logback logging framework

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
8 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Sheetal
8 years ago

for web application, this don’t works.

Gan
2 years ago
Reply to  Sheetal

Did u figure out solution for web application? I faced the same issue too

Last edited 2 years ago by Gan
Camilo Lopes
9 years ago

how can i do it works on jee project? I have tried, but the file is not created. Where should i put logback.xml file?
thanks. good post.

Balaji
10 years ago

Hi I am not able to generate the log files….though I am able to print the output in the console, could you please explain me that

bLueZ
10 years ago

Hi Mkyong, another good tutorials 🙂

But in this current time, Hibernate 4 using jboss.logging for their logging system.

In my project, i use pure Hibernate 4 and Logback only, and it seems not produce any hibernate log.

So what is the solution?
Adding logging facade using SLF4j?

Thankyou 🙂

Mick
12 years ago

Is it possible to amend to files path of the log so that you create your log in src/main/resources??? I have n app that will create some logs and then interogate them. so I would like to keep my logs on the classpath

Daniel Serodio
12 years ago
Reply to  Mick

That doesn’t make sense. “src/main/resources/” is on the “build path”, not the classpath. “target/classes/” (where Maven-compiled classes and other resources are copied to) is on the classpath.