Why aren't my SQL queries showing up in the log file with Spring Boot?

I’m trying to get my database queries to show up in a log file but they only appear in the console when I run the app.

Here’s what I have in my application.properties:

spring.datasource.url=...
spring.datasource.username=dbuser
spring.datasource.password=secret
spring.datasource.driver-class-name=com.mysql.cj.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=d:/logs/application.log

When I start the application with:

mvn spring-boot:run

The SQL queries show up fine in the terminal output, but when I check the application.log file, I only see regular Spring framework logs. The database queries are missing from the file completely.

How can I make sure the SQL statements get written to the log file along with everything else?

spring.jpa.show-sql doesnt actually use the logging framework - it prints directly to stdout thats why your seeing it in console but not logfile. try removing that and use logging.level.org.hibernate.SQL=DEBUG instead, that should route the queries through proper logging system to your file

hmm thats weird! are you sure the logging.file property is working correctly in your spring boot version? i think newer versions might need logging.file.name instead. also curious - what happens if you set logging.level.org.hibernate.SQL=DEBUG? does that make any difference for getting queries into the file?

The issue stems from how Hibernate handles SQL logging output routing. When using spring.jpa.show-sql=true, Hibernate bypasses the standard logging framework and writes directly to System.out, which explains why queries appear in your console but not in the log file. To resolve this, you need to configure proper logger-based SQL logging instead of relying on the show-sql property. Replace spring.jpa.show-sql=true with logging.level.org.hibernate.SQL=DEBUG and add logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE if you want to see parameter values as well. This approach ensures all SQL statements flow through the same logging infrastructure as your other application logs, making them appear in your specified log file alongside framework messages.