How to log SQL statements in Spring Boot

0 votes

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?

May 22, 2020 in Java by kartik
• 37,520 points
4,911 views

2 answers to this question.

0 votes

Hello @kartik,

If you hava a logback-spring.xml or something like that, add the following code to it

<logger name="org.hibernate.SQL" level="trace" additivity="false">
    <appender-ref ref="file" />
</logger>

To get bind variables as well:

<logger name="org.hibernate.type.descriptor.sql" level="trace">
    <appender-ref ref="file" />
</logger>

Hope this work!!

Get your Spring Certification today to become certified.

answered May 22, 2020 by Niroj
• 82,880 points
0 votes

HI..

try using this in your properties file:

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

Regards,

SRI

answered Sep 25, 2020 by SRI

edited Sep 25, 2020 by Sirajul

Related Questions In Java

0 votes
2 answers

How can we convert java.util.Date to java.sql.date in Java?

It's easy to convert a java.util.Date object ...READ MORE

answered Aug 27, 2019 in Java by Sirajul
• 59,230 points
1,308 views
0 votes
1 answer

How to configure port for a Spring Boot application?

Hello @kartik, You can set port in java ...READ MORE

answered May 22, 2020 in Java by Niroj
• 82,880 points
2,960 views
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 81,095 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 59,230 points
2,273 views
0 votes
1 answer

How to redirect to an external URL from controller action in Spring MVC?

Hello @kartik, You can do it with two ...READ MORE

answered May 22, 2020 in Java by Niroj
• 82,880 points

edited Mar 4, 2022 by Sarfaraz 26,794 views
0 votes
1 answer

Bean life cycle in Spring Bean Factory Container

Bean life cycle in Spring Bean Factory ...READ MORE

answered Aug 30, 2018 in Java by code.reaper12
• 3,500 points
3,259 views
0 votes
2 answers
0 votes
1 answer

How to set base url for rest in spring boot?

Hello @kartik, Set it up in your application.yml ...READ MORE

answered May 22, 2020 in Java by Niroj
• 82,880 points
14,347 views
0 votes
1 answer

How to determine programmatically the current active profile using Spring boot ?

Hello, It doesn't matter is your app Boot ...READ MORE

answered May 21, 2020 in Java by Niroj
• 82,880 points

edited Mar 4, 2022 by Sarfaraz 8,151 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP