Java 如何在 Spring Boot 中记录 SQL 语句?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30118683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to log SQL statements in Spring Boot?
提问by Oleg Pavliv
I want to log SQL statements in a file.
I have the following properties in application.properties
我想在文件中记录 SQL 语句。
我有以下属性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.
我可以在控制台中看到 sql 语句,但它们没有出现在文件 app.log 中。该文件仅包含 spring 的基本日志。
What should I do to see sql statements in the log file?
我应该怎么做才能在日志文件中看到 sql 语句?
采纳答案by Paul Woods
try using this in your properties file:
尝试在您的属性文件中使用它:
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
回答by Javier Z.
For the MS-SQL server driver (Microsoft SQL Server JDBC Driver).
对于 MS-SQL 服务器驱动程序(Microsoft SQL Server JDBC Driver)。
try using:
尝试使用:
logging.level.com.microsoft.sqlserver.jdbc=debug
in your application.properties file.
在您的 application.properties 文件中。
My personal preference is to set:
我个人的偏好是设置:
logging.level.com.microsoft.sqlserver.jdbc=info
logging.level.com.microsoft.sqlserver.jdbc.internals=debug
You can look at these links for reference:
您可以查看这些链接以供参考:
回答by v.ladynev
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
.
只需将此添加到application.properties
.
回答by Michel
This works for me (YAML):
这对我有用(YAML):
spring:
jpa:
properties:
hibernate:
show_sql: true
format_sql: true
logging:
level:
org:
hibernate:
type: trace
回答by Edye Chan
if you hava a logback-spring.xml or something like that, add the following code to it
如果你有一个 logback-spring.xml 或类似的东西,添加以下代码
<logger name="org.hibernate.SQL" level="trace" additivity="false">
<appender-ref ref="file" />
</logger>
works for me.
为我工作。
To get bind variables as well:
还要获取绑定变量:
<logger name="org.hibernate.type.descriptor.sql" level="trace">
<appender-ref ref="file" />
</logger>
回答by Max Farsikov
According to documentationit is:
根据文档,它是:
spring.jpa.show-sql=true # Enable logging of SQL statements.
回答by rahulnikhare
Please use:
请用:
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type=TRACE
spring.jpa.show-sql=true
回答by Udara S.S Liyanage
If you want to view the actual parameters used to query you can use
如果要查看用于查询的实际参数,可以使用
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql=TRACE
Then notice that actual parameter value is shown as binding parameter......
然后注意实际参数值显示为 binding parameter......
2018-08-07 14:14:36.079 DEBUG 44804 --- [ main] org.hibernate.SQL : select employee0_.id as id1_0_, employee0_.department as departme2_0_, employee0_.joining_date as joining_3_0_, employee0_.name as name4_0_ from employee employee0_ where employee0_.joining_date=?
2018-08-07 14:14:36.079 TRACE 44804 --- [ main] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [TIMESTAMP] - [Tue Aug 07 00:00:00 SGT 2018]
回答by Lova Chittumuri
We can use any one of these in application.propertiesfile:
我们可以在application.properties文件中使用以下任何一种:
spring.jpa.show-sql=true
example :
//Hibernate: select country0_.id as id1_0_, country0_.name as name2_0_ from country country0_
or
或者
logging.level.org.hibernate.SQL=debug
example :
2018-11-23 12:28:02.990 DEBUG 12972 --- [nio-8086-exec-2] org.hibernate.SQL : select country0_.id as id1_0_, country0_.name as name2_0_ from country country0_
回答by Robert.Li
Translated accepted answer to YAML works for me
已翻译的 YAML 的已接受答案对我有用
logging:
level:
org:
hibernate:
SQL:
TRACE
type:
descriptor:
sql:
BasicBinder:
TRACE