Hibernate supports different logging implementations but SLF4J-based loggers are preferred over standalone loggers. This article explains how to disable the overwhelming amount of Hibernate logs in the terminal output.
Let’s take the “JPA Hello World! using Hibernate and MySQL” project as an example.
Add Logback Dependency
To disable the logs generated by Hibernate, add the log back dependency to the pom.xml as shown below.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.javahelps.helloworld</groupId>
<artifactId>jpa-hibernate-hello-world</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hibernate.version>6.1.5.Final</hibernate.version>
<mysql.connector.java.version>8.0.31</mysql.connector.java.version>
<logback.version>1.4.5</logback.version>
</properties>
<dependencies>
<!-- Hibernate Dependency -->
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- MySQL JDBC Driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.connector.java.version}</version>
</dependency>
<!-- Logback Dependency to format Hibernate logs -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
</dependencies>
</project>
Add Logback Configuration to Disable Hibernate Logs
Create a new file named logback.xml in the src/main/resources folder with the following content.
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
<!-- Hide logback own logs -->
<statusListener class="ch.qos.logback.core.status.NopStatusListener"/>
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator"/>
<!-- Format the log output -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
</pattern>
</encoder>
</appender>
<!-- Set the application log level to INFO -->
<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>
<!-- Set log level of Hibernate to WARN level -->
<logger name="org.hibernate">
<level value="WARN"/>
</logger>
</configuration>
Save all the changes and run the project. This time, only the warning and error logs from Hibernate will be printed to the terminal. Feel free to change the log level value on line 23 to adjust the expected log level for Hibernate logs. Also, note that line 5 disables the logs generated by the Logback itself. If you want to turn them on, remove line 5.
If you find this article useful, please share your thoughts below. Knowing someone finds these articles useful motivates me to write more. If you have any questions or issues with disabling the Hibernate logs, you can ask your questions in the comments. Java Helps community will try our best to answer your questions.