Show Hibernate/JPA SQL Statements from Spring Boot | Code Factory

Reference Link : Link

Donate : Link

1. Overview

2. Logging JPA Queries

2.1. To Standard Output

spring.jpa.show-sql=true

To beautify or pretty print the SQL, we can add :

spring.jpa.properties.hibernate.format_sql=true

While this is extremely simple, it’s not recommended as it directly unloads everything to standard output without any optimizations of a logging framework.

Moreover, it doesn’t log the parameters of prepared statements.

2.2. Via Loggers

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

The first line logs the SQL queries, and the second statement logs the prepared statement parameters.

The pretty print property will work in this configuration as well.

By setting these properties, logs will be sent to the configured appender. By default, Spring Boot uses logback with a standard out appender.

3. Logging JdbcTemplate Queries

logging.level.org.springframework.jdbc.core.JdbcTemplate=DEBUG
logging.level.org.springframework.jdbc.core.StatementCreatorUtils=TRACE

Similar to the JPA logging configuration, the first line is for logging statements and the second one is to log parameters of prepared statements.

4. How Does it Work?

However, the level of those log statements is set to DEBUG and TRACE respectively, which is lower than the default level in Spring Boot — INFO.

By adding these properties, we are just setting those loggers to the required level.