Comment configurer la journalisation dans Hibernate - Logback

Comment configurer la journalisation dans Hibernate - Logback

Dans ce tutoriel, nous montrons comment intégrerLogback logging framework avec Hibernate.

Outils et technologies utilisés dans ce didacticiel:

  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. Obtenez SLF4j + Logback

Pour utiliser la déconnexion dans l'application Web Hibernate, vous avez besoin de 3 bibliothèques:

  1. slf4j-api.jar

  2. logback-core

  3. logback-classic

Fichier: pom.xml


    
        
            JBoss repository
            http://repository.jboss.org/nexus/content/groups/public/
        
    

    

        
            org.hibernate
            hibernate-core
            3.6.3.Final
        

        
        
            ch.qos.logback
            logback-core
            0.9.28
        

        
            ch.qos.logback
            logback-classic
            0.9.28
        

    

Where is slf4j-api.jar?
Leslf4j-api.jar est défini comme la dépendance de «hibernate-core», vous n'avez donc pas besoin de le déclarer à nouveau.

2. logback.xml

Créez un fichier «logback.xml» et placez-le dans le chemin de classe de votre projet, voir la figure ci-dessous:

logback hibernate

Un exemple classique de «logback.xml».



 
    
        %d{yyyy-MM-dd_HH:mm:ss.SSS} %-5level %logger{36} - %msg%n
                
    
 

 
    c:/exampleapp.log
    
       %d{yyyy-MM-dd_HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
           
    

    
        c:/exampleapp.%i.log.zip
        1
        10
    

    
        2MB
    

  

  
  

  
    
    
  

Avec cette configuration delogback.xml, cela signifie, redirigez toutes les sorties de journalisation de votre application Web vers la console et également un fichier à «c:/exampleapp.log».

3. Sortie

Voir la sortie de journalisation de la journalisation à «C:\exampleapp.log» ci-dessous:

//...
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.example.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

Téléchargez-le -Logback-Hibernate-Example.zip (8 Ko)