本文翻译自:How to log SQL statements in Spring Boot?
I want to log SQL statements in a file. 我想将SQL语句记录在文件中。
I have the following properties in application.properties
我在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. 我可以在控制台中看到sql语句,但是它们未出现在文件app.log中。 The file contains only basic logs from spring. 该文件仅包含来自spring的基本日志。
What should I do to see sql statements in the log file? 如何查看日志文件中的sql语句?
#1楼
参考:https://stackoom.com/question/22nfd/如何在Spring-Boot中记录SQL语句
#2楼
try using this in your properties file: 尝试在属性文件中使用它:
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
#3楼
For the MS-SQL server driver (Microsoft SQL Server JDBC Driver). 对于MS-SQL服务器驱动程序(Microsoft SQL Server JDBC驱动程序)。
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: 您可以查看以下链接以供参考:
- https://msdn.microsoft.com/en-us/library/ms378517(v=sql.110).aspx https://msdn.microsoft.com/zh-CN/library/ms378517(v=sql.110).aspx
- https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html
#4楼
This works for stdout too: 这也适用于stdout:
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: 要记录值:
spring.jpa.properties.hibernate.type=trace
Just add this to application.properties
. 只需将其添加到application.properties
。
#5楼
This works for me (YAML): 这对我有用(YAML):
spring:
jpa:
properties:
hibernate:
show_sql: true
format_sql: true
logging:
level:
org:
hibernate:
type: trace
#6楼
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>