java 无法避免使用 Spring Boot 和 Logback 将 SQL 休眠记录到控制台
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36496178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't avoid hibernate logging SQL to console with Spring Boot and Logback
提问by garci560
My Spring Boot application keeps showing Hibernate queries in the console despite having configured Hibernate's specific logging with Logback as follows:
尽管使用 Logback 配置了 Hibernate 的特定日志记录,我的 Spring Boot 应用程序仍然在控制台中显示 Hibernate 查询,如下所示:
<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.
它确实将 Hibernate 的日志(包括查询)发送到文件hibernate.log
. 但我也想避免控制台中的查询,我认为这种配置应该会发生。
What am I missing?
我错过了什么?
回答by Ali Dehghani
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:
如果您将 设置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 );
}
}
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
:
因此,如果您不想在控制台上看到查询,只需hibernate.show_sql
将其设置为禁用false
或完全删除即可。在 Spring Boot 中,只需将其添加到您的application.properties
:
spring.jpa.show-sql=false
回答by eztup
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
我只是想分享一下,我刚刚注意到有另一个设置可能会导致 org.hibernate.SQL 在 Spring Boot JUnit 测试中进行调试,尽管您可能已经设置
spring.jpa.show-sql=false
and
和
spring.jpa.properties.hibernate.show_sql=false
...
...
If you set
如果你设置
debug=true
in your Spring application*.properties file!
在您的 Spring 应用程序*.properties 文件中!
This one set to true will override the show-sql setting and set it to true.
设置为 true 将覆盖 show-sql 设置并将其设置为 true。
Brgds
桥接器
回答by Mahaveer Jangir
You basically need to set 2 properties to false.
您基本上需要将 2 个属性设置为 false。
If you are using Spring boot , then set up in Application.properties as below
如果您使用的是 Spring boot ,则在 Application.properties 中进行如下设置
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
如果您使用的是 hibernate.cfg.xml,则设置如下
<property name="hibernate.generate_statistics">false</property>
<property name="show_sql">false</property>
回答by Sparm
In case anyone has tried all the above and still has issues, try setting the property:
如果有人尝试了以上所有方法但仍有问题,请尝试设置属性:
logging.level.org.hibernate.SQL=OFF
回答by Ethiraj
My application is a spring-boot one,
我的应用程序是一个 spring-boot 应用程序,
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.
出于某种原因,在我的情况下,属性 'spring.jpa.show-sql=false' 不起作用。我可以看到带有绑定值的 sql 继续在控制台/日志中打印查询。
It is resolved by changing the root level to error like below
它通过将根级别更改为如下错误来解决
<root level="ERROR">
<appender-ref ref="STDOUT"/>
</root>
<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>
<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
谢谢