W3docs

How can I log SQL statements in Spring Boot?

To log SQL statements in Spring Boot, you can configure the logging level of the org.hibernate.SQL logger to DEBUG or TRACE.

To log SQL statements in Spring Boot, you can configure the logging level of the org.hibernate.SQL logger to DEBUG or TRACE.

Here's an example of how you can do this in an application.properties file:


logging.level.org.hibernate.SQL=DEBUG

This will log all SQL statements executed by Hibernate at the DEBUG level.

To also log the parameter values bound to these statements, you can enable BasicBinder tracing:


logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

Note that spring.jpa.properties.hibernate.show_sql is deprecated in Hibernate 6. The recommended approach is to rely on the logging configuration above. In older versions, you could use spring.jpa.show-sql=true, which logs SQL at the INFO level by default and may be verbose for production environments.

Note that these configurations will only log SQL statements executed by Hibernate. If you want to log all SQL statements executed by the database, you will need to configure the database driver to log the statements.