35

My Spring Boot application keeps showing Hibernate queries in the console despite having configured Hibernate's specific logging with Logback as follows:

<appender name="HIBERNATE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOGDIR}/hibernate.log</file>
    <encoder>
        <pattern>%d{yyyy-MM-dd HH:mm:ss} - %msg%n</pattern>
    </encoder>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>${LOGDIR}/hibernate.log.%d</fileNamePattern>
    </rollingPolicy>
</appender>

<logger name="org.hibernate" additivity="false">
    <appender-ref ref="HIBERNATE"/>
</logger>

<logger name="org.hibernate.SQL" additivity="false">
    <appender-ref ref="HIBERNATE"/>
</logger>

<logger name="org.hibernate.type.descriptor.sql" additivity="false">
    <appender-ref ref="HIBERNATE"/>
</logger>

It does send Hibernate's logs, including queries, to the file hibernate.log. But I would also like to avoid the queries in the console, which I think should be happening with this configuration.

What am I missing?

1
  • Is hibernate.show_sql config enabled? If so, just disable it Apr 8, 2016 at 9:38

5 Answers 5

30

If you set the hibernate.show_sql to true, Hibernate will simply print the SQL statement to the console (not to be confused with logging under org.hibernate.SQL). SqlStatementLogger is responsible for logging the SQL statements and its logStatement looks like:

public void logStatement(String statement, Formatter formatter) {
    if ( format ) {
        if ( logToStdout || LOG.isDebugEnabled() ) {
            statement = formatter.format( statement );
        }
    }
    LOG.debug( statement );
    if ( logToStdout ) {
        System.out.println( "Hibernate: " + statement );
    }
}

So, if you do not want to see the queries on the console, just disable the hibernate.show_sql by setting it to false or just removing it altogether. In Spring Boot, just add this to your application.properties:

spring.jpa.show-sql=false
4
  • 1
    didn't think it could be so simple. Thanks
    – garci560
    Apr 8, 2016 at 10:22
  • LOG.isDebugEnabled() means it also controlled by logger configuration such as logback
    – xiaoyifang
    May 27, 2016 at 5:50
  • this solution solved my problem ... more specifically the code: spring.jpa.show-sql=false
    – Digao
    Apr 2, 2017 at 17:47
  • 3
    In Spring Boot, as asked, spring.jpa.properties.hibernate.show_sql=false is the one property that needs to be set to false as answered by Mahaveer Jangir.
    – JRA_TLL
    Nov 13, 2018 at 13:57
29

I just wanted to share that I just noticed there is another setting which could cause org.hibernate.SQL to debug in Spring Boot JUnit tests, though you might have set

spring.jpa.show-sql=false 

and

spring.jpa.properties.hibernate.show_sql=false

...

If you set

debug=true

in your Spring application*.properties file!

This one set to true will override the show-sql setting and set it to true.

Brgds

15

You basically need to set 2 properties to false.

If you are using Spring boot , then set up in Application.properties as below

spring.jpa.properties.hibernate.generate_statistics=false
spring.jpa.properties.hibernate.show_sql=false

And If you are using hibernate.cfg.xml, then set up as below

<property name="hibernate.generate_statistics">false</property>
<property name="show_sql">false</property>
1
  • 1
    This really works. I am fed up with Hibernate: ... logs. Thanks.
    – WesternGun
    Oct 1, 2019 at 16:07
8

In case anyone has tried all the above and still has issues, try setting the property:

logging.level.org.hibernate.SQL=OFF
1
  • The only that worked using Springboot 2.4.3 Mar 4, 2021 at 12:31
0

My application is a spring-boot one,

For some reason in my case the property 'spring.jpa.show-sql=false' didn't work. I could see the sql with the bind values keep on printing the queries in the console/log.

It is resolved by changing the root level to error like below

<root level="ERROR"> <appender-ref ref="STDOUT"/> </root>

Though we change log level to error in the root, we still can print our package/application logs in the info mode my using the separate loggers like below

<logger name="com.application.code" level="INFO"> <appender-ref ref="FILE"/> <appender-ref ref="ERR_FILE"/> </logger>

putting here so it might help someone.

Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.