首页 文章

如何在spring-boot中记录sql语句

提问于
浏览
212

我是 Spring 季靴子的新手 .

我想在文件中记录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

当我运行我的应用程序

cmd>mvn spring-boot:run

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

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

9 回答

  • 294

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

    logging.level.org.hibernate.SQL=DEBUG
    logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
    
  • 6

    这对我有用(YAML):

    spring:
      jpa:
        properties:
          hibernate:
            show_sql: true
            format_sql: true
    logging:
      level:
        org:
          hibernate:
            type: trace
    
  • 7

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

    spring.jpa.show-sql=true 
    
    example :
    //Hibernate: select country0_.id as id1_0_, country0_.name as name2_0_ from country country0_
    

    要么

    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_
    
  • 133

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

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

    然后注意实际参数值显示为 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]
    
  • 1

    这也适用于stdout:

    spring.jpa.properties.hibernate.show_sql=true
    spring.jpa.properties.hibernate.use_sql_comments=true
    spring.jpa.properties.hibernate.format_sql=true
    

    要记录值:

    spring.jpa.properties.hibernate.type=trace
    

    只需将其添加到 application.properties 即可 .

  • 55

    请用:

    logging.level.org.hibernate.SQL=DEBUG
    logging.level.org.hibernate.type=TRACE
    spring.jpa.show-sql=true
    
  • 0

    根据documentation它是:

    spring.jpa.show-sql=true # Enable logging of SQL statements.
    
  • 5

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

    尝试使用:

    logging.level.com.microsoft.sqlserver.jdbc=debug
    

    在您的application.properties文件中 .

    我个人的偏好是:

    logging.level.com.microsoft.sqlserver.jdbc=info
    logging.level.com.microsoft.sqlserver.jdbc.internals=debug
    

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

  • 10

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

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

    适合我 .

    要获取绑定变量:

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

相关问题