How to save Spring Boot SQL queries in a log file?

I’m trying to capture SQL queries in a log file with Spring Boot. Here’s what I’ve done:

In my config.properties, I set up:

db.connection=...
db.user=admin
db.pass=pass123
db.driver=com.example.jdbc.CustomDriver

hibernate.show_sql=true
hibernate.format_sql=true

log.security=false
log.level.web=INFO
log.level.hibernate=INFO
log.file=/var/log/myapp.log

When I start the app with gradle bootRun, I can see the SQL in the terminal. But myapp.log only has basic Spring stuff, not the SQL.

How do I get those SQL queries into the log file? Am I missing something obvious? Thanks for any help!

To capture SQL queries in your log file, you need to configure the logging levels for Hibernate specifically. Add these lines to your application.properties or application.yml file:

logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql=TRACE

Also, ensure your log file configuration is correct:

logging.file.name=/var/log/myapp.log

This setup will log SQL queries and their parameters to your specified file. Remember to restart your application after making these changes. If you’re using a custom logging framework, you might need to adjust these settings accordingly.

hey there! have u considered using a logging framework like logback? it’s super flexible and works great with spring boot. u could configure it to log SQL queries to a specific file. what logging setup are u currently using? maybe we could brainstorm some ideas to get those queries captured!

hey luke, i’ve had this issue before. try adding these to ur properties:

logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

this should dump the SQL queries into ur log file. lmk if it works!