日志记录使程序员能够将日志详细信息永久写入文件。这是我们以后在开发当中非常重要的一步。

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]

Hibernate 使用log4j日志记录的更多相关文章

  1. Hibernate使用Log4j日志记录(使用properties文件)

    我们知道,Log4j和Logback框架可用于支持日志记录hibernate,使用log4j有两种执行日志记录的方法: 通过log4j.xml文件(或) 通过log4j.properties文件 在这 ...

  2. Hibernate使用Log4j日志记录(使用xml文件)

    日志记录使程序员能够将日志详细信息永久写入文件. Log4j和Logback框架可以在hibernate框架中使用来支持日志记录. 使用log4j执行日志记录有两种方法: 通过log4j.xml文件( ...

  3. ELK环境配置+log4j日志记录

    ELK环境配置+log4j日志记录 1. 背景介绍 在大数据时代,日志记录和管理变得尤为重要. 以往的文件记录日志的形式,既查询起来又不方便,又造成日志在服务器上分散存储,管理起来相当麻烦, 想根据一 ...

  4. java Web应用配置log4j日志记录

    公司启动全国支票影像系统,有这样一个功能是和消息中间件打交道,需要记录每一个报文出错情况,因为方便后期开发人员的调试,我选用了log4j作为日志记录,好了废话不多说了! 第一步:首先建立一个WEB工程 ...

  5. log4j日志记录级别是如何工作?

    级别p的级别使用q,在记录日志请求时,如果p>=q启用.这条规则是log4j的核心.它假设级别是有序的.对于标准级别它们关系如下:ALL < DEBUG < INFO < WA ...

  6. ELK菜鸟手记 (一) 环境配置+log4j日志记录

    1. 背景介绍 在大数据时代,日志记录和管理变得尤为重要. 以往的文件记录日志的形式,既查询起来又不方便,又造成日志在服务器上分散存储,管理起来相当麻烦, 想根据一个关键字查询日志中某个关键信息相当困 ...

  7. 在android中配置 slf4j + log4j 日志记录框架

    需求: 在项目开发中,需要记录 操作日志 .起初自己写了个简单的日志记录文本写入到文本的方法,后来随着项目的膨胀,需要考虑更多的操作,开始考虑性能问题. 实现: 考虑使用 slf4j + log4j ...

  8. 配置spring的log4j日志记录

    1.导入依赖包pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http: ...

  9. Log4j日志记录

    1.导入log4j的jar包 2.写log4j.properties文件,配置日志记录参数,一般参数如下所示: 第二行指定了输出日志的目录,此处用的相对路径,也可换成绝对路径: 第三行指定了输出的记录 ...

随机推荐

  1. 回文链表 · Palindrome Linked List

    [抄题]: 设计一种方式检查一个链表是否为回文链表.1->2->1 就是一个回文链表. [暴力解法]: 时间分析: 空间分析: [思维问题]: 以为要从从后往前扫描,不知道调用revers ...

  2. HEIDSOFT

    HEIDSOFT ENTHUSIASTIC GITHUB USER heidsoft@sina.com GitHub Profile I'm a developer based in China.sh ...

  3. Github 使用的Markdown语言

    简介 官方站点:http://daringfireball.net/projects/markdown/syntax 中文介绍:http://www.worldhello.net/gotgithub/ ...

  4. RotbotFrameWork接口测试

    一.添加接口测试Library 二.参数说明 三.关键字使用说明 Set Varibae: 参数化接口地址 Connect To Databse Using Custom Param: 连接数据库(需 ...

  5. js常用utils

    var utils = { /** * 日期格式化 * * @param {Date} date 指定日期 * @param {String} format * @returns {String} * ...

  6. app里面嵌套的H5页面上电话号码怎么才能点击拨号?

    代码: <p>联系我们:<a href="tel://66666666666">66666666666</a> </p>

  7. JavaScript面向对象编程小游戏---贪吃蛇

    1 面向对象编程思想在程序项目中有着非常明显的优势: 1- 1 代码可读性高.由于继承的存在,即使改变需求,那么维护也只是在局部模块 1- 2 维护非常方便并且成本较低. ​ 2 这个demo是采用了 ...

  8. 谷歌浏览器插件开发入门-官方版Helloworld详解

    目录: 需求 原理 实现步骤: 一个空的插件 一个可以设置一种背景色的插件(可以设置百度首页的背景色为绿色) 一个可以设置多种背景色的插件 需求: 插件可以改变特定网址的背景颜色. 原理: 将各种ht ...

  9. oracle通过job执行procedure

    1. 先创建一个FUNCTION CREATE OR REPLACE FUNCTION GET_TIMEOUT_PROGRAM(i_customerNo IN TK_CUST_PROG_D.CUSTO ...

  10. django drf 深入ModelSerializer

    serializer用起来稍微麻烦,可以使用ModelSerializer,类似于django里的Form与ModelForm 1.定义ModelSerializer from rest_framew ...