Hibernate Tutorial
Core Hibernate
Hibernate Mapping
Hibernate Annotations
Hibernate with Spring Framework
Hibernate with Database
Hibernate Log4j
Inheritance Mapping
Logging is an essential aspect of application development, especially when dealing with frameworks like Hibernate. Log4j is a popular Java-based logging utility that integrates seamlessly with Hibernate. In this tutorial, we'll learn how to configure Hibernate logging using Log4j via a properties file.
You need to have the Log4j and Hibernate dependencies in your project. If you are using Maven, add the following dependencies to your pom.xml
:
<!-- Hibernate Core --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.4.30.Final</version> <!-- use the latest version --> </dependency> <!-- Log4j API --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.14.1</version> <!-- use the latest version --> </dependency> <!-- Log4j Core (implementation) --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.14.1</version> <!-- use the latest version --> </dependency>
log4j.properties
File:Create a log4j.properties
file in your src/main/resources
directory with the following contents:
# Root logger option log4j.rootLogger=INFO, stdout # Direct log messages to stdout 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{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n # Hibernate logging options log4j.logger.org.hibernate=INFO # Echo all SQL log4j.logger.org.hibernate.SQL=DEBUG log4j.logger.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
This configuration will:
INFO
.DEBUG
level on org.hibernate.SQL
).In your Hibernate configuration (hibernate.cfg.xml
or persistence.xml
), ensure that you have the following property set:
<property name="hibernate.show_sql">false</property>
By setting hibernate.show_sql
to false
, you're letting Log4j handle SQL logging rather than Hibernate's basic built-in mechanism.
If you are using a standalone application, Log4j should initialize itself using the log4j.properties
file when your application starts up.
For web applications, ensure that the Log4j initialization runs when your web context starts. Most modern servlet containers will handle this automatically when they detect Log4j in the classpath.
Run your application, perform some database operations using Hibernate, and observe the logging output in your console. You should see Hibernate's log messages and SQL statements being printed.
Configuring Hibernate to use Log4j for logging via a properties file is straightforward. Proper logging is crucial for troubleshooting, performance tuning, and understanding the behavior of your application. Adjusting the logging levels as shown in the log4j.properties
file allows you to get fine-grained control over the logging output, which can be invaluable in both development and production environments.
Log4j properties file for Hibernate logging:
# log4j.properties # Root logger option log4j.rootLogger=INFO, console, file # Console appender configuration log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n # File appender configuration log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.File=logs/hibernate.log log4j.appender.file.MaxFileSize=5MB log4j.appender.file.MaxBackupIndex=10 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
Logging Hibernate SQL queries with Log4j:
# Enable SQL logging for Hibernate log4j.logger.org.hibernate.SQL=DEBUG