无法避免使用Spring Boot和Logback进行休眠日志记录SQL [英] Can't avoid hibernate logging SQL to console with Spring Boot and Logback

查看:578
本文介绍了无法避免使用Spring Boot和Logback进行休眠日志记录SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

< logger name =org.hibernateadditivity =false>
< appender-ref ref =HIBERNATE/>
< / logger>

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

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

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



我缺少什么?

$ b $如果您将 hibernate.show_sql 设置为 true $ c>,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);




$ b $ p
$ b $ p所以,如果你不想看到查询在控制台上,只需将 hibernate.show_sql 设置为 false 或者完全删除它即可。在Spring Boot中,只需将它添加到 application.properties

  spring.jpa.show-sql = false 


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?

解决方案

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

这篇关于无法避免使用Spring Boot和Logback进行休眠日志记录SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆