如何在日志文件而不是控制台中记录Hibernate sql语句

时间:2018-10-27 12:22:06

标签: hibernate spring-boot

我需要将Hibernate sql语句登录到日志文件而不是控制台中。 我正在使用Spring Boot JPA。和:

spring.jpa.show-sql=true
logging.path=/logs

1 个答案:

答案 0 :(得分:0)

请在配置中添加以下几行(how to log sql statements in spring-boot

#show sql statement
logging.level.org.hibernate.SQL=debug
#show sql values
logging.level.org.hibernate.type.descriptor.sql=trace

spring.jpa.show-sql原因语句记录到标准输出Hibernate show sql is equal to Stdout

如果您希望Hibernate日志使用其他文件,则可以添加logback.xml。并为org.hibernate logger定义文件附加器

<configuration>
     <appender name="fileAppender"
                  class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>${DEV_HOME}/[yourlognamefile].log</file>
            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                <Pattern>
                    %d{yyyy-MM-dd HH:mm:ss} - %msg%n
                </Pattern>
            </encoder>

            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <!-- rollover daily -->
                <fileNamePattern>${DEV_HOME}/archived/[yourlognamefile].%d{yyyy-MM-dd}.%i.log
                </fileNamePattern>
                <timeBasedFileNamingAndTriggeringPolicy
                        class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                    <maxFileSize>10MB</maxFileSize>
                </timeBasedFileNamingAndTriggeringPolicy>
            </rollingPolicy>

        </appender>

    <logger name="org.hibernate" level="DEBUG" />
         <appender-ref ref="fileAppender"/>
    </logger>
</configuration>

有关更多信息,您可以看看: https://dzone.com/articles/configuring-logback-with-spring-boot