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.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Master Hibernate Logging in Spring Boot 3: From SQL to Statistics

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: true

Setting 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]': true

Enables pretty‑printed SQL for easier reading.

2.3 Highlight SQL

spring:
  jpa:
    properties:
      hibernate:
        '[highlight_sql]': true

Activates ANSI color codes to visually distinguish keywords and tables in supported consoles.

2.4 Show SQL Comments

spring:
  jpa:
    properties:
      hibernate:
        '[use_sql_comments]': true

Includes additional comments in generated SQL, which can be customized with @Meta annotations.

2.5 Generate Statistics

spring:
  jpa:
    properties:
      hibernate:
        '[generate_statistics]': true

Enables 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]': INFO

Logs a warning when a query exceeds the specified threshold (200 ms in the example).

2.7 Log Raw SQL

logging:
  level:
    '[org.hibernate.SQL]': DEBUG

Outputs 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]': TRACE

When set to TRACE, Hibernate logs the bound values for each placeholder.

2.9 Log Transaction Events

logging:
  level:
    '[org.hibernate.engine.transaction]': DEBUG

Shows transaction lifecycle events such as begin, commit, and rollback.

2.10 Log Result Set Extraction

logging:
  level:
    '[org.hibernate.orm.jdbc.extract]': TRACE

Provides 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]': DEBUG

Outputs comprehensive statistics for performance analysis.

2.12 Log Second‑Level and Query Cache

logging:
  level:
    '[org.hibernate.cache]': DEBUG

Shows 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.

SQLSpring BootHibernatejpa
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

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.