Master Hibernate Logging in Spring Boot 3: From SQL to Statistics
This article explains how to configure and customize Hibernate and Spring Data JPA logging in Spring Boot 3, covering SQL output, formatting, highlighting, comment inclusion, statistics, slow‑query detection, transaction logs, parameter binding, cache logs, and related performance diagnostics.
Introduction
Debugging database interactions in a Spring Boot application can be challenging. When using Spring Data JPA and Hibernate, logging is more than just console output; it can become a powerful diagnostic and monitoring tool.
Practical Cases
2.1 Enable SQL Logging
spring:
jpa:
show-sql: trueSetting spring.jpa.show-sql=true prints all executed SQL statements to the console, but placeholders remain without actual values.
2.2 Format SQL
spring:
jpa:
properties:
hibernate:
'[format_sql]': trueEnables pretty‑printed SQL for easier reading.
2.3 Highlight SQL
spring:
jpa:
properties:
hibernate:
'[highlight_sql]': trueActivates ANSI color codes to visually distinguish keywords and tables in supported consoles.
2.4 Show SQL Comments
spring:
jpa:
properties:
hibernate:
'[use_sql_comments]': trueIncludes additional comments in generated SQL, which can be customized with @Meta annotations.
2.5 Generate Statistics
spring:
jpa:
properties:
hibernate:
'[generate_statistics]': trueEnables Hibernate's internal statistics, providing metrics such as query count, cache hits, and entity loads.
2.6 Log Slow Queries
spring:
jpa:
properties:
hibernate:
'[log_slow_query]': 200
logging:
level:
'[org.hibernate.SQL_SLOW]': INFOLogs a warning when a query exceeds the specified threshold (200 ms in the example).
2.7 Log Raw SQL
logging:
level:
'[org.hibernate.SQL]': DEBUGOutputs the exact SQL sent to the database, without parameter values.
2.8 Log SQL Parameters
logging:
level:
'[org.hibernate.SQL]': DEBUG
'[org.hibernate.orm.jdbc.bind]': TRACEWhen set to TRACE, Hibernate logs the bound values for each placeholder.
2.9 Log Transaction Events
logging:
level:
'[org.hibernate.engine.transaction]': DEBUGShows transaction lifecycle events such as begin, commit, and rollback.
2.10 Log Result Set Extraction
logging:
level:
'[org.hibernate.orm.jdbc.extract]': TRACEProvides detailed logs for the process of extracting data from ResultSet objects.
2.11 Log Hibernate Statistics
spring:
jpa:
properties:
hibernate:
'[generate_statistics]': true
logging:
level:
'[org.hibernate.stat]': DEBUGOutputs comprehensive statistics for performance analysis.
2.12 Log Second‑Level and Query Cache
logging:
level:
'[org.hibernate.cache]': DEBUGShows cache hits, misses, and evictions, useful during development.
Conclusion
By selectively enabling these logging options, developers can gain deep insight into Hibernate’s behavior, diagnose performance bottlenecks, and fine‑tune their Spring Boot applications. Remember to disable heavy logging in production unless explicitly needed.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
