How can I log SQL statements in Spring Boot?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Logging SQL Statements in Spring Boot
Logging SQL statements in a Spring Boot application is often crucial for debugging, performance monitoring, and auditing. By logging SQL queries, developers can gain insights into the database interactions of their applications and identify any inefficiencies or errors in the SQL operations. Spring Boot offers several approaches to achieve SQL logging, each with its own set of benefits and configurations.
Enabling SQL Logging with Hibernate
Spring Boot uses Hibernate as the default JPA provider. Hibernate provides built-in support for SQL logging that can be enabled by setting specific properties in your application.properties or application.yml file.
Configuration in application.properties
To enable SQL logging with Hibernate in Spring Boot, you can modify your application.properties like so:
spring.jpa.show-sql: This property allows you to log the SQL statements generated by Hibernate.spring.jpa.properties.hibernate.format_sql: Formats the SQL output to make it more readable, with line breaks and proper indentation.spring.jpa.properties.hibernate.use_sql_comments: Includes comments in the SQL for better context and understanding.
Example Output
When you enable SQL logging and run your application, you will see SQL statements similar to the following example in the console:
Advanced SQL Logging with DataSource Proxy
For more comprehensive logging, including details such as execution time, you can use tools like DataSource-Proxy. DataSource-Proxy wraps your existing DataSource and intercepts calls to log detailed information.
Adding DataSource-Proxy to Your Project
First, add the DataSource-Proxy library to your pom.xml if you are using Maven:
Configuring DataSource-Proxy
Create a configuration class for the proxy:
This configuration intercepts calls to the DataSource and logs SQL statements along with execution specifics.
Example Detailed Log
With DataSource-Proxy set up, you will receive detailed logs, which might include:
SQL Logging at Different Levels
Spring Boot allows you to control the level of SQL logging by using the logging framework's configuration. If you are using SLF4J with Logback, you can configure the verbosity of your SQL logs.
Example Logback Configuration
Add or edit the src/main/resources/logback-spring.xml file to include:
With this configuration:
org.hibernate.SQL: Logs the SQL statements at the DEBUG level.org.hibernate.type.descriptor.sql.BasicBinder: Logs the SQL binding parameters at the TRACE level, providing insight into the values used in SQL statements.
Summary Table
| Method | Description | Configuration File | Benefits |
| Hibernate Properties | Logs plain SQL statements without parameters | application.properties | Simple setup and basic SQL logging |
| DataSource-Proxy | Logs SQL with execution time and parameters | Custom configuration | Detailed logging and execution time |
| Logback Configuration | Logs SQL at different verbosity levels with specific logger configurations | logback-spring.xml | Fine control over logging verbosity |
Conclusion
Enabling and configuring SQL logging in a Spring Boot application can greatly improve your visibility into how your application interacts with databases. Depending on your needs, Spring Boot offers basic logging with Hibernate configurations, as well as more advanced solutions using DataSource-Proxy and logging framework configurations. Through these various methods, you can tailor the SQL logging to suit your specific requirements and gain valuable insights into your application's database interactions.
Related reading
- How can I make a JPA OneToOne relation lazy
- How can I make SQL case sensitive string comparison on MySQL?
- How can I modify the size of column in a MySQL table?
- How can i optimize MySQL's ORDER BY RAND function?
- How can I make a timeout for a crushed server
- How can I measure the actual memory usage of an application or process?
- How can I lookup a Java enum from its String value?
- How can I measure the speed of code written in Java? AI algorithms

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.