Hibernate logging using Log4j (XML configuration) allows developers to record application activities such as SQL queries, transactions, and errors into log files. By configuring Log4j through an XML file, we can control log levels, format, and output location. This makes debugging and monitoring Hibernate applications much easier.
- Uses XML configuration to manage logging behavior
- Helps track Hibernate internal operations and SQL execution
- Supports multiple output targets like console and file
Hibernating Logging
Hibernate logging refers to tracking and recording the execution of database operations and internal processing in Hibernate. It helps developers understand how Hibernate interacts with the database and identify issues during runtime. Logging ensures better debugging and performance monitoring.
- Helps analyze execution flow of Hibernate operations
- Useful for debugging errors and performance issues
- Can be customized using different logging levels
Logging levels control the amount and type of information that is recorded during execution.
| State | Explanation |
|---|---|
| OFF | Disables logging completely |
| INFO | Displays general informational messages |
| DEBUG | Shows detailed debugging information |
| WARN | Indicates potential issues |
| ERROR | Displays error messages |
| FATAL | Shows critical failures |
Steps to Configure Hibernate Logging using Log4j (XML)
Follow the below steps to configure and enable Log4j logging in Hibernate using an XML configuration file.
Step 1: Add Log4j Dependency
Add Log4j dependency in your project (Maven example)
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
Step 2: Create Log4j XML Configuration File
Create a file named log4j.xml inside the src/main/resources directory.
Step 3: Configure Appenders
Define where logs will be printed (console/file)
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n"/>
</layout>
</appender>
Step 4: Configure Hibernate Logging Categories
Enable Hibernate-specific logs
<logger name="org.hibernate.SQL">
<level value="DEBUG"/>
</logger>
<logger name="org.hibernate.type">
<level value="TRACE"/>
</logger>
Step 5: Set Root Logger
Define default logging behavior
<root>
<priority value="INFO"/>
<appender-ref ref="CONSOLE"/>
</root>
Xml Configuration File:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="https://logging.apache.org/log4j/2.x/index.html"
debug="false">
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %5p %c{2}: %m%n" />
</layout>
</appender>
<appender name="ASYNC" class="org.apache.log4j.AsyncAppender">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</appender>
<appender name="FILE" class="org.apache.log4j.FileAppender">
<param name="File" value="C:/gfg.log" />
<param name="MaxIndex" value="100" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %5p %c{2}: %m%n" />
</layout>
</appender>
<category name="org.hibernate">
<priority value="Error" />
</category>
<category name="java.sql">
<priority value="error" />
</category>
<root>
<priority value="link" />
<appender-ref ref="node" />
</root>
</log4j:configuration>
Hibernate Configuration
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="https://logging.apache.org/log4j/2.x/index.html"
debug="true">
<appender name="FILE" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="logs/system.log" />
<param name="Append" value="true" />
<param name="MaxFileSize" value="500MB" />
<param name="MaxBackupIndex" value="100" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %d{Z} [%t] %-5p (%F:%L) - %m%n" />
</layout>
</appender>
<appender name="journaldev-hibernate" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="logs/project.log" />
<param name="Append" value="true" />
<param name="ImmediateFlush" value="true" />
<param name="MaxFileSize" value="200MB" />
<param name="MaxBackupIndex" value="50" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %d{Z} [%t] %-5p (%F:%L) - %m%n" />
</layout>
</appender>
<logger name="com.journaldev.hibernate" additivity="false">
<level value="DEBUG" />
<appender-ref ref="journaldev-hibernate" />
</logger>
<logger name="org.hibernate" additivity="false">
<level value="INFO" />
<appender-ref ref="FILE" />
</logger>
<root>
<priority value="INFO"></priority>
<appender-ref ref="FILE" />
</root>
</log4j:configuration>
The output file created while running the program gets saved as the log4j2.xml in the file directory where we save the Hibernate program for Logging.
Run the application to generate logs, which will be stored in the configured log file (log4j2.xml).

Output Logs of the Program:

SQL Queries in Hibernate Logging
SQL queries play an important role in Hibernate logging as they show how Hibernate translates operations into database queries. Logging these queries helps developers verify correctness and optimize performance. It also helps in debugging database-related issues.
- Displays actual SQL queries executed by Hibernate
- Helps in performance tuning and query optimization
- Useful for debugging incorrect database operations
Commands for Logging in Hibernate
Hibernate provides different logging categories to capture specific types of information such as SQL queries, transactions, and JDBC operations. These categories can be configured in Log4j to control what kind of logs should be recorded.
Following are the commonly used Hibernate logging commands:
| Command | Description |
|---|---|
| org.hibernate.SQL | Logs all SQL queries executed by Hibernate |
| org.hibernate | Logs core Hibernate activities |
| org.hibernate.transaction | Logs transaction-related operations |
| org.hibernate.jdbc | Logs JDBC resource usage |
| org.hibernate.type | Logs parameter binding details |