user3377708
user3377708

Reputation: 161

How can I log Hibernate sql statements in a log file instead of console

I need to log Hibernate sql statement into a log file instead of console. I'm using spring boot JPA. and:

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

Upvotes: 3

Views: 4682

Answers (1)

Mehran Mastcheshmi
Mehran Mastcheshmi

Reputation: 815

Add following lines to the configuration please(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 causees that statements are logged into stdout Hibernate show sql is equal to Stdout

If you want a different file for Hibernate logs, you could add logback.xml. and define a file appender for 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>

For more information, you could have a look at: https://dzone.com/articles/configuring-logback-with-spring-boot

Upvotes: 7

Related Questions