How to Log SQL Statements in Spring Boot


In Spring Boot, there are a couple of ways to log SQL statements in order to debug and troubleshoot any issues with the application’s database interactions.

Using Application Properties

You can add the following property in your application.properties file, which will enable logging of all SQL statements:

logging.level.org.hibernate.SQL=debug
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=trace

Using YAML

logging:
  level:
    org:
      hibernate:
        SQL:
          TRACE
        type:
          descriptor:
            sql:
              BasicBinder:
                TRACE

Using Logback configuration

You can configure Logback to log the SQL statements by adding the following in your logback-spring.xml file:

<logger name="org.hibernate.SQL" level="debug"/>
<logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="trace"/>

Log in to standard output

Add to application.properties

### To enable
spring.jpa.show-sql=true

### To make the printing SQL beautify
spring.jpa.properties.hibernate.format_sql=true

It’s also worth noting that these settings may also add a performance overhead to your application