3

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
2
  • You can refer to this. stackoverflow.com/questions/1710476/… Commented Oct 27, 2018 at 12:44
  • @FarrukhAhmed unfortunately this question/answers don't address writing to log file instead of console. Commented Oct 27, 2018 at 12:49

1 Answer 1

7

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.