How to logs hibernate SQL in spring boot

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

Image by Clay Banks on Unsplash
Image by Clay Banks on Unsplash

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 file.

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

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

This blog is open-source on Github.