How can I log SQL statements in Spring Boot?

Cover Image for How can I log SQL statements in Spring Boot?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

How to Log SQL Statements in Spring Boot

Do you want to log those SQL statements in your Spring Boot application but they just won't show up in your log file? 😮 Don't worry, I've got your back! In this blog post, I'll walk you through the common issues and provide easy solutions to get those SQL statements logged.

The Setup 🛠️

Before we dive into the solutions, let's quickly recap the setup. You have your application.properties file with the necessary properties to configure your datasource and logging. Your file might look something like this:

spring.datasource.url=...
spring.datasource.username=user
spring.datasource.password=1234
spring.datasource.driver-class-name=net.sourceforge.jtds.jdbc.Driver

spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true

security.ignored=true
security.basic.enabled=false

logging.level.org.springframework.web=INFO
logging.level.org.hibernate=INFO
logging.file=c:/temp/my-log/app.log

Now, let's get to the heart of the matter!

Solution 1: Adjust Log Levels 🔍

The first thing we need to check is the log levels. In your application.properties, you've set logging.level.org.hibernate=INFO, which means only INFO log level messages from Hibernate will be logged. However, SQL statements are usually logged at the DEBUG level.

To fix this, simply update the log level for Hibernate to DEBUG:

logging.level.org.hibernate=DEBUG

Save the changes and restart your application. You should now start seeing those SQL statements in your log file! 🎉

Solution 2: Enable Spring Boot's Datasource Logging 📝

If you're still not seeing the SQL statements in your log file, it might be because Spring Boot's datasource logging is not enabled by default. But fret not! We can enable it with a few tweaks.

  1. Open your application.properties file.

  2. Add the following line:

spring.datasource.logging.enabled=true
  1. Save the changes and restart your application.

By enabling this property, Spring Boot will start logging the SQL statements to your log file, giving you all the juicy details you need for debugging. 😉

Solution 3: Use a Logging Framework 📁

If solutions 1 and 2 didn't work for you, fear not, my friend! There's another trick up our sleeves.

  1. Add the appropriate logging framework dependency to your project. For example, if you're using Log4j2, add the following Maven dependency to your pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
  1. Configure the logger to log the SQL statements in your log4j2.xml or logback.xml file. Here's an example using Log4j2:

<Configuration>
    <!-- ... your existing configurations ... -->
    <Logger name="org.hibernate.SQL" level="DEBUG"/>
    <!-- ... additional configurations ... -->
</Configuration>
  1. Save the changes, rebuild your project if necessary, and restart your application.

With this approach, you're using a logging framework to specifically log SQL statements, giving you more control and flexibility in how you capture and store them.

Spread the Word! 🗣️

Now that we've got the SQL statements flowing into your log file, it's time to celebrate! 🎉 But let's not keep this awesome knowledge to ourselves – share it with others who might be struggling with the same issue.

If you found this blog post helpful, hit that share button and spread the love! 💙

Also, if you have any other burning questions or ideas for future blog posts, let me know in the comments below. Let's keep the conversation going! 👇


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello