如何在Spring Boot中记录SQL语句?

本文翻译自: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: 您可以查看以下链接以供参考:


#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>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值