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

Ole*_*liv 291 java logging spring hibernate spring-boot

我想在文件中记录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
Run Code Online (Sandbox Code Playgroud)

当我运行我的应用程序

cmd>mvn spring-boot:run
Run Code Online (Sandbox Code Playgroud)

我可以在控制台中看到sql语句,但它们不会出现在app.log文件中.该文件仅包含spring的基本日志.

如何在日志文件中查看sql语句?

小智 409

尝试在属性文件中使用它:

logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
Run Code Online (Sandbox Code Playgroud)

  • 如果你想记录值:`logging.level.org.hibernate.type = TRACE` (62认同)
  • 但这只会记录很少的绑定值。如何从标准 API 记录值?如果我使用规范,则使用 CriteriaBuilder 创建的绑定参数没有输出。 (3认同)
  • 在我使用 spring-boot-starter-data-jpa:3.1.4 和 hibernate 6.2.9 的项目中,“logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE”无法显示值。我不得不将其更改为“logging.level.org.hibernate.orm.jdbc.bind=TRACE” (3认同)

v.l*_*nev 174

这也适用于stdout:

spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.format_sql=true
Run Code Online (Sandbox Code Playgroud)

要记录值:

spring.jpa.properties.hibernate.type=trace 
Run Code Online (Sandbox Code Playgroud)

只需将其添加到application.properties.

  • 如果你想记录值:`spring.jpa.properties.hibernate.type = trace` (9认同)
  • 我仍然只看到`?`而不是参数.该解决方案应该向我展示吗? (3认同)
  • 这不会写入日志文件,而是写入 STDOUT (2认同)

Vla*_*cea 94

避免设置

您不应使用此设置:

spring.jpa.show-sql=true 
Run Code Online (Sandbox Code Playgroud)

问题show-sql在于 SQL 语句打印在控制台中,因此无法像通常使用 Logging 框架那样过滤它们。

使用 Hibernate 日志记录

在您的日志配置文件中,如果您添加以下记录器:

<logger name="org.hibernate.SQL" level="debug"/>
Run Code Online (Sandbox Code Playgroud)

然后,Hibernate 将在PreparedStatement创建JDBC 时打印 SQL 语句。这就是为什么将使用参数占位符记录语句的原因:

INSERT INTO post (title, version, id) VALUES (?, ?, ?)
Run Code Online (Sandbox Code Playgroud)

如果要记录绑定参数值,只需添加以下记录器:

<logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="trace"/>
Run Code Online (Sandbox Code Playgroud)

设置BasicBinder记录器后,您将看到绑定参数值也被记录:

DEBUG [main]: o.h.SQL - insert into post (title, version, id) values (?, ?, ?)
TRACE [main]: o.h.t.d.s.BasicBinder - binding parameter [1] as [VARCHAR] - [High-Performance Java Persistence, part 1]
TRACE [main]: o.h.t.d.s.BasicBinder - binding parameter [2] as [INTEGER] - [0]
TRACE [main]: o.h.t.d.s.BasicBinder - binding parameter [3] as [BIGINT] - [1]
Run Code Online (Sandbox Code Playgroud)

使用数据源代理

数据源代理OSS框架允许你代理实际的JDBC DataSource,通过如下图所示:

数据源代理

您可以定义dataSourceHibernate 将使用的bean,如下所示:

@Bean
public DataSource dataSource(DataSource actualDataSource) {
    SLF4JQueryLoggingListener loggingListener = new SLF4JQueryLoggingListener();
    loggingListener.setQueryLogEntryCreator(new InlineQueryLogEntryCreator());
    return ProxyDataSourceBuilder
        .create(actualDataSource)
        .name(DATA_SOURCE_PROXY_NAME)
        .listener(loggingListener)
        .build();
}
Run Code Online (Sandbox Code Playgroud)

请注意,actualDataSource必须DataSource由您在应用程序中使用的 [连接池][2] 定义。

接下来,您需要在日志框架配置文件中设置net.ttddyy.dsproxy.listener日志级别debug。例如,如果您使用的是 Logback,则可以添加以下记录器:

<logger name="net.ttddyy.dsproxy.listener" level="debug"/>
Run Code Online (Sandbox Code Playgroud)

启用后datasource-proxy,SQL 语句将被记录如下:

Name:DATA_SOURCE_PROXY, Time:6, Success:True,
Type:Prepared, Batch:True, QuerySize:1, BatchSize:3,
Query:["insert into post (title, version, id) values (?, ?, ?)"],
Params:[(Post no. 0, 0, 0), (Post no. 1, 0, 1), (Post no. 2, 0, 2)]
Run Code Online (Sandbox Code Playgroud)


Mic*_*hel 84

这对我有用(YAML):

spring:
  jpa:
    properties:
      hibernate:
        show_sql: true
        format_sql: true
logging:
  level:
    org:
      hibernate:
        type: trace
Run Code Online (Sandbox Code Playgroud)

  • 我一直以为`show_sql: true`属于`jpa` (2认同)

小智 17

请用:

logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type=TRACE
spring.jpa.show-sql=true
Run Code Online (Sandbox Code Playgroud)

  • `logging.level.org.hibernate.SQL = DEBUG`使它对我有用,而其他答案则不见了。谢谢! (4认同)

Edy*_*han 15

如果你有一个logback-spring.xml或类似的东西,请添加以下代码

<logger name="org.hibernate.SQL" level="trace" additivity="false">
    <appender-ref ref="file" />
</logger>
Run Code Online (Sandbox Code Playgroud)

适合我.

要获取绑定变量:

<logger name="org.hibernate.type.descriptor.sql" level="trace">
    <appender-ref ref="file" />
</logger>
Run Code Online (Sandbox Code Playgroud)


sto*_*ter 13

对于休眠 6:它是:

spring.jpa.properties.hibernate.show_sql=true
logging.level.org.hibernate.orm.jdbc.bind = trace
Run Code Online (Sandbox Code Playgroud)

  • 我针对使用 hibernate 6.1.5 的 Spring Boot 3.0.0 验证了这一点。不过,对我来说,我使用了“logging.level.org.hibernate.SQL=DEBUG”和“logging.level.org.hibernate.orm.jdbc.bind=TRACE”。 (3认同)

小智 9

对于MS-SQL服务器驱动程序(Microsoft SQL Server JDBC驱动程序).

尝试使用:

logging.level.com.microsoft.sqlserver.jdbc=debug
Run Code Online (Sandbox Code Playgroud)

在您的application.properties文件中.

我个人的偏好是:

logging.level.com.microsoft.sqlserver.jdbc=info
logging.level.com.microsoft.sqlserver.jdbc.internals=debug
Run Code Online (Sandbox Code Playgroud)

您可以查看以下链接以供参考:


小智 9

已翻译的 YAML 的已接受答案对我有用

logging:
  level:
    org:
      hibernate:
        SQL:
          TRACE
        type:
          descriptor:
            sql:
              BasicBinder:
                TRACE
Run Code Online (Sandbox Code Playgroud)

  • 如果您不想嵌套一次性道具,也可以在 YAML 中使用平面属性,例如: `logging.level.org.hibernate.SQL: TRACE` `logging.level.org.hibernate.type.descriptor.sql .BasicBinder:跟踪` (5认同)

小智 9

我们可以在 Spring boot 中使用两种方法记录 SQL 语句: 1:使用 logger 2:标准方法


对于记录器, 您应该将此行添加到 application.properties 文件中:

logging.level.org.hibernate.SQL=DEBUG
Run Code Online (Sandbox Code Playgroud)

标准方法 您应该在 application.properties 文件中添加这些行:

spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
Run Code Online (Sandbox Code Playgroud)


Max*_*kov 8

根据文件,它是:

spring.jpa.show-sql=true # Enable logging of SQL statements.
Run Code Online (Sandbox Code Playgroud)


Sav*_*ake 7

Log in to standard output

Add to application.properties

### to enable
spring.jpa.show-sql=true
### to make the printing SQL beautify
spring.jpa.properties.hibernate.format_sql=true
Run Code Online (Sandbox Code Playgroud)

This the simplest way to print the SQL queries though it doesn't log the parameters of prepared statements. And its is not recommended since its not such as optimized logging framework.

Using Logging Framework

Add to application.properties

### logs the SQL queries
logging.level.org.hibernate.SQL=DEBUG
### logs the prepared statement parameters
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
### to make the printing SQL beautify
spring.jpa.properties.hibernate.format_sql=true
Run Code Online (Sandbox Code Playgroud)

By specifying above properties, logs entries will be sent to the configured log appender such as log-back or log4j.


小智 7

我能够通过在application.properties文件中添加这些变量行来解决该问题:

logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.orm.jdbc.bind=TRACE
spring.jpa.properties.hibernate.format_sql=true
Run Code Online (Sandbox Code Playgroud)

这使我能够看到调试 SQL 查询(以格式化的形式)和传递的变量(参数)值。

我的日志输出是这样的:

17:21:45.965 [http-nio-7001-exec-5] DEBUG org.hibernate.SQL - 
    select
        a1_0.action_id,
        a1_0.action_nr,
        a1_0.app_version
    from
        action a1_0 
    where
        a1_0.guid=? limit ?
17:21:45.965 [http-nio-7001-exec-5] TRACE org.hibernate.orm.jdbc.bind - binding parameter [1] as [VARCHAR] - [60154cd5-3f51-4c45-9c65-2af724c5c693]
17:21:45.965 [http-nio-7001-exec-5] TRACE org.hibernate.orm.jdbc.bind - binding parameter [2] as [INTEGER] - [1]
Run Code Online (Sandbox Code Playgroud)


Uda*_*age 6

如果要查看用于查询的实际参数,可以使用

logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql=TRACE
Run Code Online (Sandbox Code Playgroud)

然后注意实际参数值显示为 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]
Run Code Online (Sandbox Code Playgroud)


Lov*_*uri 6

我们可以在application.properties文件中使用以下任何一种:

spring.jpa.show-sql=true 

example :
//Hibernate: select country0_.id as id1_0_, country0_.name as name2_0_ from country country0_
Run Code Online (Sandbox Code Playgroud)

或者

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_
Run Code Online (Sandbox Code Playgroud)