Hibernate Tutorial

Core Hibernate

Hibernate Mapping

Hibernate Annotations

Hibernate with Spring Framework

Hibernate with Database

Hibernate Log4j

Inheritance Mapping

Hibernate - Logging By Log4j Using Properties File

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.

1. Adding Dependencies:

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>

2. Configure Log4j with a 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:

  • Output log messages to the console.
  • Set the default logging level for Hibernate to INFO.
  • Print out the SQL statements Hibernate is executing (due to the DEBUG level on org.hibernate.SQL).

3. Configure Hibernate:

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.

4. Initialize Log4j:

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.

5. Test It Out:

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.

Conclusion:

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.

  1. Log4j properties file for Hibernate logging:

    • Description: Create a Log4j properties file to configure logging settings for Hibernate. The file includes configurations for appenders, log levels, and other settings.
    • Code:
      # 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
      
  2. Logging Hibernate SQL queries with Log4j:

    • Description: Enable logging of Hibernate SQL queries by adjusting the Log4j configuration. Set the log level for the specific Hibernate category to include SQL statements.
    • Code:
      # Enable SQL logging for Hibernate
      log4j.logger.org.hibernate.SQL=DEBUG