Configure Hibernate Logging with slf4j + log4j2

摘要: This tutorial shows how to configure hibernate with slf4j and log4j2. Logging is an important tool for each developer. So it’s worth setting up right the first time. We configure two logging appenders. The first is for logging to the console, the second is for logging to a file for archival reasons.

This tutorial shows how to configure hibernate with slf4j and log4j2. Logging is an important tool for each developer. So it’s worth setting up right the first time. We configure two logging appenders. The first is for logging to the console, the second is for logging to a file for archival reasons.

Maven Dependencies

We are using Apache Maven to manage the projects dependencies. Add the following dependencies to your projects pom.xml file.

<!-- database -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>6.0.4</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.2.3.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-c3p0</artifactId>
    <version>5.2.3.Final</version>
</dependency>

<!-- logging -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.7</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-1.2-api</artifactId>
    <version>2.7</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.21</version>
</dependency>

Project Structure

Make sure thelog4j2.xml configuration file resides on the classpath in the src/main/resources folder.

src
|    +--main
|        +--java
|            +--com
|                +--memorynotfound
|                    +--hibernate
|                        |--App.java
|                        |--Book.java
|                        |--HibernateUtil.java
|        +--resources
|            |--Hibernate.cfg.xml
|            |--log4j2.xml
pom.xml

Log4j2 Configuration

In the log4j2.xml file, we can configure log4j2. In this example we define two loggers. The first will write the output to the console. The second will write the output to a file for archival reasons. We can specify the log level using the Logger element. Which is located inside the Loggers element. In this element we can provide a package name to manage the log level of that specific package.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorinterval="30" status="info" strict="true">
    <Properties>
        <Property name="filename">log/app.log</Property>
    </Properties>
    <Appenders>
        <Appender type="Console" name="Console">
            <Layout type="PatternLayout" pattern="%d %p [%t] %m%n" />
        </Appender>
        <Appender type="File" name="File" fileName="${filename}">
            <Layout type="PatternLayout" pattern="%d %p %C{1.} [%t] %m%n" />
        </Appender>
    </Appenders>
    <Loggers>
        <Logger name="com.memorynotfound" level="debug"/>
        <Logger name="com.mchange.v2.c3p0" level="warn"/>
        <Root level="info">
            <AppenderRef ref="File" />
            <AppenderRef ref="Console" />
        </Root>
    </Loggers>
</Configuration>

Output

The following output is an example of the log, created by log4j2.

2016-10-12 10:53:53,947 INFO [main] HHH000412: Hibernate Core {5.2.3.Final}
2016-10-12 10:53:53,950 INFO [main] HHH000206: hibernate.properties not found
2016-10-12 10:53:53,951 INFO [main] HHH000021: Bytecode provider name : javassist
2016-10-12 10:53:54,122 INFO [main] HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2016-10-12 10:53:54,228 INFO [main] HHH010002: C3P0 using driver: com.mysql.cj.jdbc.Driver at URL: jdbc:mysql://localhost:3306/bookstore?serverTimezone=Europe/Brussels
2016-10-12 10:53:54,228 INFO [main] HHH10001001: Connection properties: {user=root, password=****}
2016-10-12 10:53:54,229 INFO [main] HHH10001003: Autocommit mode: false
2016-10-12 10:53:54,241 INFO [main] MLog clients using log4j logging.
2016-10-12 10:53:54,347 INFO [main] HHH10001007: JDBC isolation level: 
2016-10-12 10:53:54,655 INFO [main] HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
2016-10-12 10:53:54,699 INFO [main] HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
2016-10-12 10:53:55,156 INFO [main] HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.[email protected]abff8b7] 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.
2016-10-12 10:53:55,168 INFO [main] HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.[email protected]4492eede] 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.
2016-10-12 10:53:55,181 INFO [main] HHH000476: Executing import script 'org.hiber[email protected]5580d62f'
2016-10-12 10:53:55,260 DEBUG [main] creating new book
2016-10-12 10:53:55,261 DEBUG [main] saving new book
2016-10-12 10:53:55,294 DEBUG [main] committing transaction
2016-10-12 10:53:55,299 DEBUG [main] closing session
2016-10-12 10:53:55,300 DEBUG [main] shutting down
2016-10-12 10:53:55,301 INFO [main] HHH000477: Starting delayed drop of schema as part of SessionFactory shut-down'
2016-10-12 10:53:55,301 INFO [main] HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.[email protected]68d651f2] 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.

Download

上一篇: Hibernate Dbcp Connection Pooling Configuration
下一篇: Configure Hibernate Logging with SLF4j + Logback
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号