In this Hibernate tutorial, I will guide you how to configure your Java project to use log4j version 2.x with Hibernate framework.

By default, Hibernate uses JBoss logging API to print logging information to the standard console, something like this:

Hibernate default logging

Besides the default logging, Hibernate also allows programmers to plugin external logging framework if they wish to have control over the logging process. And in this post, you will learn how to use log4j2 (log4j version 2.x) with Hibernate.

 

1. Specify log4j2 dependency

First, you need to add log4j2’s JAR files to the project’s classpath, namely log4j2-api-2.x.x.jar and log4j2-core-2.x.x.jar. You can download log4j2 JAR files here.

In case you use Maven, specify the following dependencies in the pom.xml file:

<dependency>
	<groupId>org.apache.logging.log4j</groupId>
	<artifactId>log4j-api</artifactId>
	<version>2.12.1</version>
</dependency>
<dependency>
	<groupId>org.apache.logging.log4j</groupId>
	<artifactId>log4j-core</artifactId>
	<version>2.12.1</version>
</dependency>
When Hibernate detects log4j2’s JAR files present in the classpath, it will look for configuration file, either log4j2.xml or log4j2.properties.

 

2. Configure Hibernate logging using log4j2 via log4j2.xml file



Create the log4j2.xml file under the src/main/resources directory of your project as follows:

log4j2 xml in project

And put the following content in to the log4j2.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
  <Appenders>

    <!-- Console Appender -->
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n" />
    </Console>
    
  </Appenders>
  <Loggers>
    <!-- Log everything in hibernate -->
    <Logger name="org.hibernate" level="info" additivity="false">
      <AppenderRef ref="Console" />
    </Logger>

    <!-- Log all SQL statements -->
    <Logger name="org.hibernate.SQL" level="debug" additivity="false">
      <AppenderRef ref="Console" />      
    </Logger>

    <Root level="error">
      <AppenderRef ref="Console" />      
    </Root>
  </Loggers>
</Configuration>
This is a simple log4j configuration that prints log information to the standard output console (Console appender). Two Hibernate log categories used are org.hibernate and org.hibernate.SQL:

<!-- Log everything in hibernate -->
<Logger name="org.hibernate" level="info" additivity="false">
  <AppenderRef ref="Console" />
</Logger>

<!-- Log all SQL statements -->
<Logger name="org.hibernate.SQL" level="debug" additivity="false">
  <AppenderRef ref="Console" />      
</Logger>
You can see the complete list of Hibernate log categories here.

Run the project and you will see the default log format is replaced by log4j’s:

hibernate log with log4j2

To also write log information to a file, use the following configuration:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
  <Appenders>

    <!-- Console Appender -->
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n" />
    </Console>

    <!-- File Appender -->
    <File name="File" fileName="hibernate.log">
      <PatternLayout pattern="%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n" />
    </File>
    
  </Appenders>
  <Loggers>
    <!-- Log everything in hibernate -->
    <Logger name="org.hibernate" level="info" additivity="false">
      <AppenderRef ref="Console" />
      <AppenderRef ref="File" />
    </Logger>

    <!-- Log SQL statements -->
    <Logger name="org.hibernate.SQL" level="debug" additivity="false">
      <AppenderRef ref="Console" />
      <AppenderRef ref="File" />
    </Logger>

    <Root level="error">
      <AppenderRef ref="Console" />
      <AppenderRef ref="File" />
    </Root>
  </Loggers>
</Configuration>
This will print all log information to the log file named hibernate.log, using the file appender:

<!-- File Appender -->
<File name="File" fileName="hibernate.log">
	<PatternLayout pattern="%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n" />
</File>
To change the log layout pattern, refer to this tutorial.


3. Configure Hibernate logging using log4j via log4j2.properties file

In case you want to use properties file instead of XML, create the log4j2.properties file under the src/main/resources directory of your project as follows:

log4j2 properties file in project

And put the following content:

appenders = console, file
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

appender.file.type = File
appender.file.name = File
appender.file.fileName = app.log
appender.file.layout.type = PatternLayout
appender.file.layout.pattern = [%-5level] %d{dd-MM-yyyy HH:mm:ss.SSS} [%t] %c{1} - %msg%n

rootLogger.level = info
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT
rootLogger.appenderRef.file.ref = File

logger.hibernate.name=org.hibernate.SQL
logger.hibernate.level=debug
As you can see in this log4j2.properties file we use two appenders, one for console and one for file, and we specify a logger for the Hibernate log category org.hibernate.SQL that logs all SQL statements. You can see the complete list of Hibernate log categories here.

NOTE: If you use the log category org.hibernate.SQL, you should set the hibernate.show_sql property to false in Hibernate configuration file.

You can watch the video version below:

 

That’s how to enable Hibernate logging using log4j2. Check the following tutorials to learn more about Hibernate framework:

 

Other Hibernate Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment

   


Comments 

#2Shams2021-02-17 13:41
Great article. Do you know how I can modify log4j2.properties to see parameters in the sql statements?
Quote
#1Mikhail2020-04-11 10:16
Why there are two log messages ?
Quote