Hibernateでのログインの設定方法 - Logback

Hibernateでログを構成する方法–ログバック

このチュートリアルでは、Logback logging frameworkをHibernateと統合する方法を示します。

このチュートリアルで使用されるツールとテクノロジー:

  1. Hibernate 3.6.3。最終

  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. SLF4j +ログバックを取得

Hibernate Webアプリケーションでログバックを使用するには、3つのライブラリが必要です。

  1. slf4j-api.jar

  2. logback-core

  3. logback-classic

ファイル: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?
slf4j-api.jarは「hibernate-core」の依存関係として定義されているため、再度宣言する必要はありません。

2. logback.xml

logback.xml」ファイルを作成し、プロジェクトのクラスパスに配置します。次の図を参照してください。

logback hibernate

古典的な「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
    

  

  
  

  
    
    
  

このlogback.xml構成では、すべてのWebアプリケーションのログ出力をコンソールと「c:/exampleapp.log」のファイルにリダイレクトします。

3. 出力

以下の「C:\exampleapp.log」のログバックログ出力を参照してください。

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

ダウンロード–Logback-Hibernate-Example.zip(8KB)