Hibernate Logging by Log4j using xml File (original) (raw)
Last Updated : 9 Apr, 2026
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)
log4j log4j 1.2.17
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)
Step 4: Configure Hibernate Logging Categories
Enable Hibernate-specific logs
Step 5: Set Root Logger
Define default logging behavior
Xml Configuration File:
XML `
<log4j:configuration xmlns:log4j="https://logging.apache.org/log4j/2.x/index.html"
debug="false">
`
Hibernate Configuration
XML `
<log4j:configuration xmlns:log4j="https://logging.apache.org/log4j/2.x/index.html" debug="true">
<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>
`
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).

Program Directory for Log4j using XML
**Output Logs of the Program:

The Output Logs of the Program using logging in Hibernate
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 |