以Spring Boot 为例
Maven POM文件 引入

<dependency>
    <groupId>com.googlecode.log4jdbc</groupId>
    <artifactId>log4jdbc</artifactId>
    <version>1.2</version>
</dependency>

数据源配置 将原有驱动改为net.sf.log4jdbc.DriverSpy url前缀添加jdbc:log4-

    driverClassName: net.sf.log4jdbc.DriverSpy
    url: jdbc:log4jdbc:mysql://ip:port/db?useCompression=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false

最后在log level 屏蔽不需要的日志
logging:
    level:
    jdbc.sqlonly: warn
    jdbc.sqltiming: info
    jdbc.resultsettable: info
    jdbc.resultset: warn
    jdbc.connection: warn
    jdbc.audit: warn

20220705
druid 开启完整sql打印

datasource:
    druid:
     filters: stat,wall,slf4j
     filter:
       slf4j:
         enabled: true
         statementCloseAfterLogEnabled: false
         statementCreateAfterLogEnabled: false
         statementExecutableSqlLogEnable: true
         statementExecuteAfterLogEnabled: false
         statementExecuteBatchAfterLogEnabled: false
         statementExecuteQueryAfterLogEnabled: false
         statementExecuteUpdateAfterLogEnabled: false
         statementLogEnabled: true
         statementParameterSetLogEnabled: false
         statementPrepareAfterLogEnabled: false
         statementPrepareCallAfterLogEnabled: false
         resultSetLogEnabled: false

logging:
  file.path: /applog/${spring.application.name}
  level:
    druid:
      sql:
        Statement: DEBUG

宇宙第一帅
33 声望1 粉丝