Partner – DBSchema – NPI (tag=SQL)
announcement - icon

DbSchema is a super-flexible database designer, which can take you from designing the DB with your team all the way to safely deploying the schema.

The way it does all of that is by using a design model, a database-independent image of the schema, which can be shared in a team using GIT and compared or deployed on to any database.

And, of course, it can be heavily visual, allowing you to interact with the database using diagrams, visually compose queries, explore the data, generate random data, import data or build HTML5 database reports.

>> Take a look at DBSchema

Partner – Trifork – NPI (cat=Spring Boot)
announcement - icon

Navigating the complexities of Spring can be difficult, even for seasoned developers.

If you need direct, practical help and guidance with your own Spring work, Trifork's CTO, Joris Kuipers, is running a closed-door call.

It's free, but it's limited to only 3 seats, so if you need it, I would join quickly and be sure to attend:

>>> CTO Spring Open Office Hour Session - Technical Guidance

With more than 15 years of leading custom software development projects involving Spring, Joris has gained a lot of real-world experience, and this call is about sharing and helping the community.


Course – LSD (cat=Persistence)

Get started with Spring Data JPA through the reference Learn Spring Data JPA course:


1. Overview

Spring JDBC and JPA provide abstractions over native JDBC APIs, allowing developers to do away with native SQL queries. However, we often need to see those auto-generated SQL queries and the order in which they were executed for debugging purposes.

In this quick tutorial, we're going to look at different ways of logging these SQL queries in Spring Boot.

Further reading:

Spring JDBC

Introduction to the Spring JDBC abstraction, with example on how to use the JbdcTempalte and NamedParameterJdbcTemplate APIs.

Introduction to Spring Data JPA

Introduction to Spring Data JPA with Spring 4 - the Spring config, the DAO, manual and generated queries and transaction management.

Hibernate Interceptors

A quick and practical guide to creating Hibernate interceptors.

2. Logging JPA Queries

2.1. To Standard Output

The simplest way to dump the queries to standard out is to add the following to

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

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

Now let's see how we can log the SQL statements by configuring loggers in the properties file:

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

To configure statement logging when using JdbcTemplate, we need the following properties:

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

4. How Does It Work?

The Spring/Hibernate classes, which generate SQL statements and set the parameters, already contain the code for logging them.

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.

5. Conclusion

In this short article, we've looked at the ways to log SQL queries in Spring Boot.

If we choose to configure multiple appenders, we can also separate SQL statements and other log statements into different log files to keep things clean.

Course – LSD (cat=Persistence)

Get started with Spring Data JPA through the reference Learn Spring Data JPA course:

res – Persistence (eBook) (cat=Persistence)
Inline Feedbacks
View all comments
Comments are closed on this article!