简体   繁体   English

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

[英]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:尽管使用 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 .它确实将 Hibernate 的日志(包括查询)发送到文件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?我错过了什么?

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 ).如果您将hibernate.show_sql设置为true ,Hibernate 将简单地将 SQL 语句打印到控制台(不要与org.hibernate.SQL下的日志记录混淆)。 SqlStatementLogger is responsible for logging the SQL statements and its logStatement looks like: 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.因此,如果您不想在控制台上看到查询,只需将hibernate.show_sql设置为false或将其完全删除即可禁用它。 In Spring Boot, just add this to your application.properties :在 Spring Boot 中,只需将其添加到您的application.properties

spring.jpa.show-sql=false

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桥接器

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>

如果有人尝试了以上所有方法但仍有问题,请尝试设置属性:

logging.level.org.hibernate.SQL=OFF

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.出于某种原因,在我的情况下,属性 'spring.jpa.show-sql=false' 不起作用。 I could see the sql with the bind values keep on printing the queries in the console/log.我可以看到带有绑定值的 sql 继续在控制台/日志中打印查询。

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谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM