Hibernate學習:slf4j日誌框架

十五樓亮哥發表於2015-02-05

一:首先來看一個圖



commons-logging和slf4j都是日誌的介面,供使用者使用,而沒有提供實現!後面的log4j,slf4j-nop等才是他們的實現。


二:Hibernate框架的slf4j-api-1.5.8.jar

Hibernate預設用的是slf4j-nop.jar日誌實現方式。

但是我們可以替換成log4j的實現。但不是簡單的加上log4j-1.2.17.jar就行了。中間還需要一個轉換器slf4j-log4j12-1.5.8.jar



然後在src目錄下加上log4j.properties


### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###

log4j.rootLogger=warn, stdout

#log4j.logger.org.hibernate=info
#log4j.logger.org.hibernate=debug

### log HQL query parser activity
#log4j.logger.org.hibernate.hql.ast.AST=debug

### log just the SQL
#log4j.logger.org.hibernate.SQL=debug

### log JDBC bind parameters ###
#log4j.logger.org.hibernate.type=info
#log4j.logger.org.hibernate.type=debug

### log schema export/update ###
log4j.logger.org.hibernate.tool.hbm2ddl=debug

### log HQL parse trees
#log4j.logger.org.hibernate.hql=debug

### log cache activity ###
#log4j.logger.org.hibernate.cache=debug

### log transaction activity
#log4j.logger.org.hibernate.transaction=debug

### log JDBC resource acquisition
#log4j.logger.org.hibernate.jdbc=debug

### enable the following line if you want to track down connection ###
### leakages when using DriverManagerConnectionProvider ###
#log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace


日誌輸出:

22:12:18,627  INFO SchemaUpdate:155 - Running hbm2ddl schema update
22:12:18,629  INFO SchemaUpdate:167 - fetching database metadata
22:12:18,630  INFO SchemaUpdate:179 - updating schema
22:12:18,647  INFO TableMetadata:65 - table found: hibernate.teacher
22:12:18,647  INFO TableMetadata:66 - columns: [id, title, name]
22:12:18,647  INFO TableMetadata:68 - foreign keys: []
22:12:18,647  INFO TableMetadata:69 - indexes: [primary]
22:12:18,648  INFO SchemaUpdate:217 - schema update complete
Hibernate: insert into Teacher (name, title, id) values (?, ?, ?)



相關文章