Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't avoid hibernate logging SQL to console with Spring Boot and Logback

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?

like image 772
garci560 Avatar asked Apr 08 '16 09:04

garci560


People also ask

How do I enable hibernate logging in spring boot?

There are two ways to enable hibernate logs in the spring boot application. The JPA spring boot module will allow the logging of the underlying ORM tools. Since hibernate is the default ORM tool used for spring boot, JPA logs will enable hibernate logs.


1 Answers

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 
like image 138
Ali Dehghani Avatar answered Oct 22 '22 12:10

Ali Dehghani