2

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
user3377708
  • 151
  • 6
  • 17
  • You can refer to this. https://stackoverflow.com/questions/1710476/how-to-print-a-query-string-with-parameter-values-when-using-hibernate – Farrukh Ahmed Oct 27 '18 at 12:44
  • @FarrukhAhmed unfortunately this question/answers don't address writing to log file instead of console. – user3377708 Oct 27 '18 at 12:49

1 Answers1

6

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

Mehran Mastcheshmi
  • 785
  • 1
  • 4
  • 12