Hibernate Tutorial

Core Hibernate

Hibernate Mapping

Hibernate Annotations

Hibernate with Spring Framework

Hibernate with Database

Hibernate Log4j

Inheritance Mapping

Hibernate - Logging by Log4j using xml File

Configuring Hibernate's logging with Log4j using an XML configuration file offers more flexibility and structure than a properties file. Here's a step-by-step tutorial on how to achieve this:

1. Dependencies:

Ensure that you have the necessary dependencies for Hibernate and Log4j in your project. If you are using Maven, add these 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 log4j2.xml:

Create a log4j2.xml file in your src/main/resources directory. Here's a basic configuration that logs Hibernate's messages and SQL:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="org.hibernate" level="info">
            <AppenderRef ref="Console"/>
        </Logger>
        <Logger name="org.hibernate.SQL" level="debug">
            <AppenderRef ref="Console"/>
        </Logger>
        <Logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="trace">
            <AppenderRef ref="Console"/>
        </Logger>
        <Root level="error">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

3. Configure Hibernate:

In your Hibernate configuration (hibernate.cfg.xml or persistence.xml), ensure you have the following:

<property name="hibernate.show_sql">false</property>

This is important to prevent Hibernate's default SQL logging and let Log4j handle it instead.

4. Initialize Log4j:

In standalone applications, Log4j should initialize itself using the log4j2.xml file automatically upon application start.

For web applications, modern servlet containers should automatically initialize Log4j if it's present in the classpath. If not, you might need to add a context listener in your web.xml:

<listener>
    <listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
</listener>

5. Testing:

Run your application and interact with the database using Hibernate. You should see Hibernate's messages and the SQL being executed, formatted according to your log4j2.xml setup.

Conclusion:

Configuring Log4j for Hibernate through an XML file provides a structured and flexible approach to logging. This setup allows you to fine-tune your logging preferences, which is valuable for development, debugging, and even production monitoring. Adjust the log levels and appenders as needed to suit your specific requirements.

  1. Log4j XML configuration file for Hibernate logging:

    • Description: Configuring Log4j using an XML file to manage logging in a Hibernate application.
    • Code Example:
      <!-- log4j.xml -->
      <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
      
          <appender name="fileAppender" class="org.apache.log4j.FileAppender">
              <param name="File" value="hibernate.log" />
              <layout class="org.apache.log4j.PatternLayout">
                  <param name="ConversionPattern" value="%d %-5p %c{1}:%L - %m%n" />
              </layout>
          </appender>
      
          <root>
              <priority value="debug" />
              <appender-ref ref="fileAppender" />
          </root>
      
      </log4j:configuration>
      
  2. Hibernate log level configuration using Log4j XML:

    • Description: Configuring log levels (e.g., debug, info, warn) for Hibernate logs using Log4j XML.
    • Code Example:
      <logger name="org.hibernate">
          <level value="debug" />
      </logger>
      
  3. Logging Hibernate SQL queries with Log4j XML:

    • Description: Enabling SQL query logging in Hibernate using Log4j XML configuration.
    • Code Example:
      <logger name="org.hibernate.SQL">
          <level value="debug" />
      </logger>
      
  4. Configuring Log4j XML console appender for Hibernate logging:

    • Description: Setting up a console appender in Log4j XML to direct Hibernate logs to the console.
    • Code Example:
      <appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
          <layout class="org.apache.log4j.PatternLayout">
              <param name="ConversionPattern" value="%d %-5p %c{1}:%L - %m%n" />
          </layout>
      </appender>
      <root>
          <priority value="debug" />
          <appender-ref ref="consoleAppender" />
      </root>