Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log SQL statements in Spring Boot?

I want to log SQL statements in a file.
I have the following properties in application.properties

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 

When I run my application

cmd>mvn spring-boot:run 

I can see sql statements in the console but they don't appear in a file app.log. The file contains only basic logs from spring.

What should I do to see sql statements in the log file?

like image 305
Oleg Pavliv Avatar asked May 08 '15 07:05

Oleg Pavliv


People also ask

Should you log SQL statements?

Logging SQL queries is very important, as it allows you to validate the number of generated statements, the syntax of the auto-generated queries, as well as prove that JDBC batching works as expected.

How do I enable SQL logging in Hibernate?

Hibernate has build-in a function to enable the logging of all the generated SQL statements to the console. You can enable it by add a “show_sql” property in the Hibernate configuration file “ hibernate. cfg. xml “.


2 Answers

try using this in your properties file:

logging.level.org.hibernate.SQL=DEBUG logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE 
like image 177
Paul Woods Avatar answered Oct 15 '22 09:10

Paul Woods


This works for stdout too:

spring.jpa.properties.hibernate.show_sql=true spring.jpa.properties.hibernate.use_sql_comments=true spring.jpa.properties.hibernate.format_sql=true 

To log values:

logging.level.org.hibernate.type=trace 

Just add this to application.properties.

like image 20
v.ladynev Avatar answered Oct 15 '22 11:10

v.ladynev