一开始对hibernate日志信息不是很了解,日志也就是在运行的时候把关于hibernate的一下信息以日志的形式显示出来。
Hibernate使用Apache commons-logging来为各种事件记录日志.
先熟悉一下Hibernate的日志消息. 在不失可读性的前提下,使Hibernate的日志可能地详细. 这是必要的查错利器. 最令人感兴趣的日志分类有如下这些:
在使用Hibernate开发应用程序时, 你应当总是为org.hibernate.SQL
开启debug
级别的日志记录,或者开启hibernate.show_sql
属性。
期中实现slf接口的实例有多种,包括slf4j、log4j、jdk logging api和apache common-log等具体实现
我们选择log4j
Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件、甚至是套接口服务器、NT的事件记录器、UNIX Syslog守护进程等;我们也可以控制每一条日志的输出格式;通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程。最令人感兴趣的就是,这些可以通过一个配置文件来灵活地进行配置,而不需要修改应用的代码。
1、首先在我们的jar包中或许有slf4j的jar包,为防止冲突,先删除slf4j的相关jar包
2、然后导入log4j的jar包
3、由于是slf接口,所以需要导入转换包slf4j-log4j
4、最后需要添加log4j的配置文件log4j.properties,可以在hibernate自带的事例性文档中复制,可在project->etc
复制到工程的src下,运行自己的程序或者工程
在Eclipse 中的后台有一大堆输出:
有关于我们使用的hibernate的信息、使用的JDBC、connection、dialect、hbm2ddl、相关jar包、后面还包括数据库建表语句等
以下选取部分:
10:04:02,472 INFO Version:15 - Hibernate Annotations 3.4.0.GA
10:04:02,861 INFO Environment:560 - Hibernate 3.3.2.GA
10:04:03,470 DEBUG Configuration:1435 - connection.driver_class=com.mysql.jdbc.Driver
10:04:03,470 DEBUG Configuration:1435 - connection.url=jdbc:mysql://localhost:3306/hibernate
10:04:03,470 DEBUG Configuration:1435 - connection.username=root
10:04:03,471 DEBUG Configuration:1435 - connection.password=wuliang
10:04:03,471 DEBUG Configuration:1435 - connection.pool_size=1
10:04:03,472 DEBUG Configuration:1435 - dialect=org.hibernate.dialect.MySQLDialect
10:04:03,472 DEBUG Configuration:1435 - current_session_context_class=thread
10:04:03,472 DEBUG Configuration:1435 - cache.provider_class=org.hibernate.cache.NoCacheProvider
10:04:03,472 DEBUG Configuration:1435 - show_sql=true
10:04:03,472 DEBUG Configuration:1435 - hbm2ddl.auto=create
10:04:03,825 DEBUG AnnotationConfiguration:258 - Execute first pass mapping processing
10:04:04,002 DEBUG AnnotationConfiguration:529 - Process hbm files
10:04:04,080 INFO HbmBinder:322 - Mapping class: com.wuliang.model.Student -> student
10:04:04,141 DEBUG HbmBinder:1289 - Mapped property: id -> id
10:04:04,156 DEBUG HbmBinder:1289 - Mapped property: name -> name
10:04:04,157 DEBUG HbmBinder:1289 - Mapped property: age -> age
10:04:07,503 DEBUG SchemaExport:242 - import file not found: /import.sql
10:04:07,503 INFO SchemaExport:251 - exporting generated schema to database
10:04:07,504 DEBUG SchemaExport:377 - drop table if exists Teacher
10:04:08,118 DEBUG SchemaExport:377 - drop table if exists student
10:04:08,455 DEBUG SchemaExport:377 - create table Teacher (id integer not null, birthday varchar(255), name varchar(255), title varchar(255), primary key (id))
10:04:09,370 DEBUG SchemaExport:377 - create table student (id integer not null, name varchar(255), age integer, primary key (id))
10:04:09,768 DEBUG Printer:113 - com.wuliang.model.Teacher{id=1, birthday=null, title=mid, name=teacher}
10:04:09,772 DEBUG AbstractBatcher:410 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
10:04:09,772 DEBUG SQL:111 - insert into Teacher (birthday, name, title, id) values (?, ?, ?, ?)
Hibernate: insert into Teacher (birthday, name, title, id) values (?, ?, ?, ?)
在log4j.properties中,我们可以根据自己的需求,设置部分日志显示,只显示我们需要的日志信息即可
比如配置文件信息如下:不需要的可以在语句前面加上#,
例如:我们只需要log4j.logger.org.hibernate.tool.hbm2ddl=debug的日志信息,输出关于SQL DDL执行语句
### 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
### direct messages to file hibernate.log ###
#log4j.appender.file=org.apache.log4j.FileAppender
#log4j.appender.file.File=hibernate.log
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.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
运行之后后台输出的日志信息如下:
10:19:05,518 INFO SchemaExport:226 - Running hbm2ddl schema export
10:19:05,520 DEBUG SchemaExport:242 - import file not found: /import.sql
10:19:05,520 INFO SchemaExport:251 - exporting generated schema to database
10:19:05,521 DEBUG SchemaExport:377 - drop table if exists Teacher
10:19:05,694 DEBUG SchemaExport:377 - drop table if exists student
10:19:05,839 DEBUG SchemaExport:377 - create table Teacher (id integer not null, birthday varchar(255), name varchar(255), title varchar(255), primary key (id))
10:19:06,052 DEBUG SchemaExport:377 - create table student (id integer not null, name varchar(255), age integer, primary key (id))
10:19:06,431 INFO SchemaExport:268 - schema export complete
Hibernate: insert into Teacher (birthday, name, title, id) values (?, ?, ?, ?)