使用Spring Boot和Logback无法避免hibernate将SQL记录到控制台

尽管使用Logback配置了Hibernate的特定日志记录,我的Spring Boot应用程序仍然在控制台中显示Hibernate查询,如下所示:

 ${LOGDIR}/hibernate.log  %d{yyyy-MM-dd HH:mm:ss} - %msg%n   ${LOGDIR}/hibernate.log.%d            

它会将Hibernate的日志(包括查询)发送到文件hibernate.log 。 但是我也想避免在控制台中进行查询,我认为应该在这个配置中进行查询。

我错过了什么?

如果将hibernate.show_sql设置为true ,Hibernate将简单地将SQL语句打印到控制台(不要与org.hibernate.SQL下的日志记录混淆)。 SqlStatementLogger负责记录SQL语句,其logStatement如下所示:

 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 ); } } 

因此,如果您不想在控制台上查看查询,只需将hibernate.show_sql设置为false或仅将其删除即可禁用它。 在Spring Boot中,只需将其添加到您的application.properties

 spring.jpa.show-sql=false 

您基本上需要将2个属性设置为false。

如果您使用的是Spring引导,则在Application.properties中进行如下设置

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

如果您使用的是hibernate.cfg.xml,则设置如下

 false false 

我只是想分享一下我刚才注意到有另一个设置可能导致org.hibernate.SQL在Spring Boot JUnit测试中调试,尽管你可能已经设置了

 spring.jpa.show-sql=false 

 spring.jpa.properties.hibernate.show_sql=false 

如果你设置

 debug=true 

在你的Spring应用程序* .properties文件中!

将此设置为true将覆盖show-sql设置并将其设置为true。

Brgds