How to logs hibernate SQL in spring boot

In spring boot application we can enable hibernate sql queries logging just by adding configuration in application.properties file

Image by Clay Banks on Unsplash https://unsplash.com/photos/pNEmlb1CMZM
Image by Clay Banks on Unsplash https://unsplash.com/photos/pNEmlb1CMZM

Log hibernate SQL query

When using Spring JPA the library write the native SQL queries for us and executes it.

Usually we never have to check what the generated queries look like, but many times when debugging to fix any bug we want to check what SQL query is getting executed and what values are getting passed in SQL query, at that time we have to ask Spring JPA or hibernate to log these SQL queries.

To do that we can just add below property in application.properties file.

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

Hibernate logs SQL queries at DEBUG level in SqlStatementLogger.java class, so by adding first line we are enabling DEBUG level logs.

Hibernate logs SQL queries parameter values at TRACE level in BasicBinder.java class, so by adding second line we are enabling TRACE level logs for given class.

This blog is open-source on Github.