Hibernate Tutorial
Core Hibernate
Hibernate Mapping
Hibernate Annotations
Hibernate with Spring Framework
Hibernate with Database
Hibernate Log4j
Inheritance Mapping
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:
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>
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>
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.
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>
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.
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.
Log4j XML configuration file for Hibernate logging:
<!-- 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>
Hibernate log level configuration using Log4j XML:
<logger name="org.hibernate"> <level value="debug" /> </logger>
Logging Hibernate SQL queries with Log4j XML:
<logger name="org.hibernate.SQL"> <level value="debug" /> </logger>
Configuring Log4j XML console appender for Hibernate logging:
<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>