Backend Development 7 min read

How to Properly Log SQL Queries in Spring Boot Without Using show_sql

This article explains why the default Spring Boot show_sql setting only prints SQL to the console, demonstrates its limitations, and provides a reliable approach using Hibernate logging and a datasource‑proxy to capture full SQL statements, parameters, and batch execution details in log files.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
How to Properly Log SQL Queries in Spring Boot Without Using show_sql

Environment: Spring Boot 3.2.5.

1. Introduction

This article shows the best way to log SQL statements when using Spring Boot. Logging SQL is essential for verifying generated queries, checking syntax, and confirming that JDBC batch operations run as expected.

2. Reject the show-sql configuration

Most Spring Boot projects use Spring Data JPA with Hibernate as the default JPA provider. Enabling spring.jpa.show-sql=true (or spring.jpa.showSql: true in YAML) merely sets the Hibernate property hibernate.show_sql , which prints SQL to System.out and does not write to log files.

<code># application.yml
spring:
  jpa:
    showSql: true

# application.properties
spring.jpa.show-sql=true</code>

The underlying code adds hibernate.show_sql = true to the JPA property map.

Note: hibernate.show_sql is equivalent to using System.out.println for SQL output.

3. Configure logging to a file

<code>logging:
  file:
    path: ./logs</code>

The log file (default name spring.log ) will receive logs, but the SQL printed by show_sql will not appear there.

4. Demonstration

<code>@Test
public void testFindById() {
    System.out.println(this.userRepository.findById(1L).orElse(null));
}</code>

Running the test shows the SQL only on the console (image).

The log file contains no SQL statements. After increasing the log level to DEBUG , the console shows a DEBUG log and a Hibernate log, and the file records only the DEBUG entry.

Therefore, using show_sql does not record SQL to log files.

5. Hibernate logging limitations

A better approach is to enable Hibernate SQL logging via the application configuration:

<code>logging:
  level:
    '[org.hibernate.SQL]': debug
    # Hibernate 6
    '[org.hibernate.orm.jdbc.bind]': trace
    # Hibernate 5
    '[org.hibernate.type.descriptor.sql]': trace</code>

Make sure to match the Hibernate version used. This logs SQL when the PreparedStatement is created and logs parameter bindings when setParameter is called. However, it logs at the preparation stage, not at executeBatch , so the number of statements sent to the database remains unclear.

6. Correct way to record SQL

The reliable solution is to proxy the JDBC datasource, which captures all SQL executed by any data‑access technology (JPA, MyBatis, etc.). Add the following dependency to enable datasource‑proxy auto‑configuration:

<code>&lt;dependency&gt;
  &lt;groupId&gt;com.github.gavlyukovskiy&lt;/groupId&gt;
  &lt;artifactId&gt;datasource-proxy-spring-boot-starter&lt;/artifactId&gt;
  &lt;version&gt;1.9.2&lt;/version&gt;
&lt;/dependency&gt;</code>

Configure the proxy logger:

<code>logging:
  level:
    '[net.ttddyy.dsproxy.listener]': debug</code>

Running the application now produces logs that include the full SQL statement, batch information, and query parameters.

All SQL, batch status, and parameters are recorded.

In summary, avoid show_sql for file logging; instead, use Hibernate logging for basic visibility and datasource‑proxy for complete, reliable SQL capture.

backendJavaSpring BootHibernatesql-loggingdatasource-proxy
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.