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 :
- Hibernate 3.6.3.Final
- slf4j-api-1.6.1
- logback-core-0.9.28
- logback-classic-0.9.28
- Eclipse 3.6
- Maven 3.0.3
1. Get SLF4j + Logback
To use logback in Hibernate web application, you need 3 libraries :
- slf4j-api.jar
- logback-core
- 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.
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 :
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)
for web application, this don’t works.
Did u figure out solution for web application? I faced the same issue too
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.
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
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 🙂
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
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.
By default, Maven will find all resources file (e.g XML file) from “src/main/resources/” folder (configurable), just how Maven works.