日志记录使程序员能够将日志详细信息永久写入文件。这是我们以后在开发当中非常重要的一步。
Hibernate使用log4j日志记录,我们需要以下几个步骤:
1.导入jar包:
(1)这是hibernate下载后自带的有,导入即可。
(2):下载地址:http://logging.apache.org/log4j/1.2/download.html
(3):下载地址:https://www.slf4j.org/download.html
2.配置文件,log4j.properties.在下载的hibernate中你搜索这个文件复制到项目classpath中就可以,就是与hibernate.cfg.xml同级目录
# # Hibernate, Relational Persistence for Idiomatic Java # # License: GNU Lesser General Public License (LGPL), version 2.1 or later. # See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. # 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 #log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L (hibernateLoadPlanWalkPath->%X{hibernateLoadPlanWalkPath}) - %m%n #log4j.appender.stdout-mdc=org.apache.log4j.ConsoleAppender #log4j.appender.stdout-mdc.Target=System.out #log4j.appender.stdout-mdc.layout=org.apache.log4j.PatternLayout #log4j.appender.stdout-mdc.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L (walk path -> %X{hibernateLoadPlanWalkPath}) - %m%n log4j.appender.unclosedSessionFactoryFile=org.apache.log4j.FileAppender log4j.appender.unclosedSessionFactoryFile.append=true log4j.appender.unclosedSessionFactoryFile.file=target/tmp/log/UnclosedSessionFactoryWarnings.log log4j.appender.unclosedSessionFactoryFile.layout=org.apache.log4j.PatternLayout log4j.appender.unclosedSessionFactoryFile.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n log4j.rootLogger=info, stdout #log4j.logger.org.hibernate.loader.plan=trace, stdout-mdc #log4j.additivity.org.hibernate.loader.plan=false #log4j.logger.org.hibernate.persister.walking=trace, stdout-mdc #log4j.additivity.org.hibernate.persister.walking=false log4j.logger.org.hibernate.tool.hbm2ddl=trace log4j.logger.org.hibernate.testing.cache=debug # SQL Logging - HHH-6833 log4j.logger.org.hibernate.SQL=debug log4j.logger.org.hibernate.type.descriptor.sql.BasicBinder=trace log4j.logger.org.hibernate.type.descriptor.sql.BasicExtractor=trace log4j.logger.org.hibernate.hql.internal.ast=debug log4j.logger.org.hibernate.sql.ordering.antlr=debug log4j.logger.org.hibernate.loader.plan2.build.internal.LoadPlanImpl=debug log4j.logger.org.hibernate.loader.plan2.build.spi.LoadPlanTreePrinter=debug log4j.logger.org.hibernate.loader.plan2.exec.spi.EntityLoadQueryDetails=debug log4j.logger.org.hibernate.engine.internal.StatisticalLoggingSessionEventListener=info log4j.logger.org.hibernate.boot.model.source.internal.hbm.ModelBinder=debug log4j.logger.org.hibernate.type.descriptor.java.JavaTypeDescriptorRegistry=debug log4j.logger.org.hibernate.action.internal.EntityAction=debug log4j.logger.org.hibernate.engine.internal.Cascade=trace ### When entity copy merge functionality is enabled using: ### hibernate.event.merge.entity_copy_observer=log, the following will ### provide information about merged entity copies. ### log4j.logger.org.hibernate.event.internal.EntityCopyAllowedLoggedObserver=debug log4j.logger.org.hibernate.testing.junit4.TestClassMetadata=info, unclosedSessionFactoryFile log4j.logger.org.hibernate.boot.model.process.internal.ScanningCoordinator=debug log4j.logger.org.hibernate.cache trace log4j.logger.org.hibernate.stat trace
这样我们就可以看到控制台日志文件了。
16:20:18,275 INFO Version:46 - HHH000412: Hibernate Core {5.3.6.Final} 16:20:18,278 INFO Environment:213 - HHH000206: hibernate.properties not found 16:20:18,438 WARN deprecation:91 - HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead. Support for obsolete DTD/XSD namespaces may be removed at any time. 16:20:18,648 DEBUG RegionFactoryInitiator:118 - Cannot default RegionFactory based on registered strategies as `[]` RegionFactory strategies were registered 16:20:18,649 DEBUG RegionFactoryInitiator:49 - Cache region factory : org.hibernate.cache.internal.NoCachingRegionFactory 16:20:18,667 INFO Version:49 - HCANN000001: Hibernate Commons Annotations {5.0.4.Final} 16:20:19,050 WARN pooling:73 - HHH10001002: Using Hibernate built-in connection pool (not for production use!) 16:20:19,059 INFO pooling:129 - HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/student?serverTimezone=UTC] 16:20:19,060 INFO pooling:138 - HHH10001001: Connection properties: {user=root, password=****} 16:20:19,060 INFO pooling:143 - HHH10001003: Autocommit mode: false 16:20:19,063 INFO DriverManagerConnectionProviderImpl:281 - HHH000115: Hibernate connection pool size: 20 (min=1) Mon Oct 08 16:20:19 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification. 16:20:19,313 INFO Dialect:157 - HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect 16:20:19,998 DEBUG SQL:94 - drop table if exists teacher Hibernate: drop table if exists teacher 16:20:20,000 INFO access:47 - HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@142eef62] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode. 16:20:20,027 DEBUG SQL:94 - create table teacher (id integer not null, name varchar(255), title varchar(255), primary key (id)) engine=MyISAM Hibernate: create table teacher (id integer not null, name varchar(255), title varchar(255), primary key (id)) engine=MyISAM 16:20:20,027 INFO access:47 - HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@30ed9c6c] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode. 16:20:20,357 INFO SchemaCreatorImpl:489 - HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@355e34c7' 16:20:20,474 DEBUG StatisticsInitiator:101 - Statistics initialized [enabled=false] 16:20:20,505 DEBUG SQL:94 - insert into teacher (name, title, id) values (?, ?, ?) Hibernate: insert into teacher (name, title, id) values (?, ?, ?) 16:20:20,519 TRACE BasicBinder:65 - binding parameter [1] as [VARCHAR] - [xxc] 16:20:20,520 TRACE BasicBinder:65 - binding parameter [2] as [VARCHAR] - [高级] 16:20:20,520 TRACE BasicBinder:65 - binding parameter [3] as [INTEGER] - [1] 16:20:20,528 INFO pooling:233 - HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/student?serverTimezone=UTC]