Enabling SQL Query Logging in Spring Boot
Fast recall
Quick revision
Core answer
? placeholders. `spring.jpa.properties.hibernate.format_sql=true` pretty-prints it so multi-table joins are readable. To see actual parameter values (usually what you need to debug), add Hibernate's bind-parameter logger: logging.level.org.hibernate.orm.jdbc.bind=TRACE on Hibernate 6 / Boot 3, or logging.level.org.hibernate.type.descriptor.sql=TRACE on Hibernate 5 / Boot 2. Never enable in production: every query produces log lines, blowing out log volume and hurting throughput. For production SQL observability, use the database's slow-query log (log_min_duration_statement on Postgres), an APM (Datadog APM, New Relic), or p6spy / datasource-proxy libraries — all capture actual execution plans and timings, not just statement text.Whether you know multiple settings are needed for useful SQL logs, and whether you understand production implications
Enabling only `show-sql` and being satisfied with `?` placeholders; leaving SQL logging on in production; not knowing the logger name changed in Hibernate 6
Mention `datasource-proxy` or `p6spy` as better dev tools, and recommend database slow-query logs + APM for production
Interview-ready guide
Interview answer
Two layers of SQL logging in Boot
spring.jpa.show-sql=true— prints SQL with?placeholdersspring.jpa.properties.hibernate.format_sql=true— pretty-prints multi-table joins readably
Seeing actual bound parameters (what you usually want)
- Hibernate 6 / Boot 3:
logging.level.org.hibernate.orm.jdbc.bind=TRACE - Hibernate 5 / Boot 2:
logging.level.org.hibernate.type.descriptor.sql=TRACE - The logger name changed — first thing that breaks when migrating
Better dev tools: `datasource-proxy` or `p6spy`
- Intercept JDBC calls and log executable SQL with values inlined
- One line per query instead of Hibernate's multi-line output
- Paste straight into
psqlto reproduce a bug
Never enable SQL logging in production
- 500 queries/sec = 500 log lines/sec just from this source
- Real throughput hit + PII leakage in parameter values
Production SQL observability — three external tools
- DB slow-query log — Postgres
log_min_duration_statement=1000logs anything over 1 s - APM (Datadog, New Relic) — bytecode-instrumented JDBC with P50/P95 per query and distributed traces
- JFR events — post-hoc analysis when something spikes at 3 AM
Rule of thumb
- Rich logging in dev, minimal in prod, rely on DB/APM/JFR for prod query visibility
Enabling SQL Query Logging in Spring Boot
Direct Answer
Two layers. spring.jpa.show-sql=true prints Hibernate-generated SQL with ? placeholders. spring.jpa.properties.hibernate.format_sql=true pretty-prints it so multi-table joins are readable. To see actual parameter values (usually what you need to debug), add Hibernate's bind-parameter logger: logging.level.org.hibernate.orm.jdbc.bind=TRACE on Hibernate 6 / Boot 3, or logging.level.org.hibernate.type.descriptor.sql=TRACE on Hibernate 5 / Boot 2. Never enable in production: every query produces log lines, blowing out log volume and hurting throughput. For production SQL observability, use the database's slow-query log (log_min_duration_statement on Postgres), an APM (Datadog APM, New Relic), or p6spy / datasource-proxy libraries — all capture actual execution plans and timings, not just statement text.
Common Mistakes & Pitfalls
Enabling only show-sql and being satisfied with ? placeholders; leaving SQL logging on in production; not knowing the logger name changed in Hibernate 6
Check your understanding
Answer these without looking back at the article.
- Why did the Hibernate bind-parameter logger name change from `org.hibernate.type` to `org.hibernate.orm.jdbc.bind`?
- How does datasource-proxy differ from Hibernate's built-in SQL logging in performance overhead?
- How would you detect N+1 queries without turning on SQL logging in production?
- What's the difference between `log_min_duration_statement` and `auto_explain` in PostgreSQL?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.