一:首先来看一个图
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 (?, ?, ?)