6

Springboot Druid 使用Slf4j输出可执行SQL

 3 years ago
source link: https://www.raye.wang/springboot-druid-ji-yu-logshu-chu-ke-zhi-xing-sql/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Springboot Druid 使用Slf4j输出可执行SQL

28 六月 2019 5:41:22 下午

在开发中,为了数据安全,所有SQL语句肯定是用占位符的,但是在实际开发中,为了方便追踪问题,经常需要查看具体执行的SQL语句内容,而用了占位符之后,每次真实执行的语句只能靠自己根据参数拼接出来,耗时耗力,所以需要一个方法输出每次执行的SQL语句,并且还是有参数的(替换了占位符),而Druid就支持此功能(Druid的主要功能可不是这个),所以此文主要讲Springboot环境下使用Druid输出可执行SQL,并且是使用的Springboot的默认日志配置格式,因为一旦修改了Springboot的日志配置格式,输出来的日志一般是没有颜色区分的,如下:

修改后的日志图片
而springboot默认配置的日志如下:
默认配置图片

当然也可以自己配置出来颜色,只是我个人觉得springboot的默认配置已经很好看了,不用再配置了

当然Springboot配置Druid输出可执行的SQL文档太多了,只是他们都需要配置日志本身,也就是配置后日志输出的样子我不喜欢,所以各种研究后,才有了此文(之前百度是真没有找到用默认配置输出的方法)

Druid 介绍

Druid是Java语言中最好的数据库连接池。Druid能够提供强大的监控和扩展功能。以上是官方的自我介绍,也侧面说明了Druid是一个功能强大,自带监控扩展的数据库连接池,具体想了解Druid的话建议去全球最大的同性交友基地了解

使用Springboot默认的日志配置文件,意味着就没有log4j.properties之类的文件给我们配置了,那就只有在application.yml或者application.properties中配置springboot告诉它我们哪些地方需要打印日志,需要打印什么级别的日志

首先要配置连接池为Druid

spring:  
  application:
    name: admin-managee
  datasource:
    druid:
      url: jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=CTT
      username: test
      password: test
      driver-class-name: com.mysql.cj.jdbc.Driver

使用Druid连接池跟之前不同的是多了一层druid,而其他的配置则是一样的

而之前用代码或者xml配置druid都在druid这个层级下,包括输出可执行SQL如下:

      filters: stat,wall,stat,slf4j,default
      web-stat-filter:
        enabled: true
        url-pattern: /*
        exclusions: "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*"
      stat-view-servlet:
        enabled: true
        url-pattern: /druid/*
        reset-enable: true
        login-username: Raye
        login-password: 123456
      filter:
        slf4j:
          enabled: true
          statement-create-after-log-enabled: false
          statement-log-enabled: false
          statement-executable-sql-log-enable: true
          statement-log-error-enabled: true
          result-set-log-enabled: false

其中只要statement-executable-sql-log-enable配置为true,则可以输出可执行SQL,当然如果不想用slf4j输出日志,用log4j只需要吧slf4j替换成log4j即可,不过还是推荐使用slf4j,至于为什么使用slf4j,百度很多人都已经说的很好了,另外druid的其他配置说明也请移步专业的druid文章,他们写的更加详细

完整配置如下:

spring:  
  application:
    name: admin-managee
  datasource:
    druid:
      url: jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=CTT
      username: test
      password: test
      driver-class-name: com.mysql.cj.jdbc.Driver
      filters: stat,wall,stat,slf4j,default
      web-stat-filter:
        enabled: true
        url-pattern: /*
        exclusions: "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*"
      stat-view-servlet:
        enabled: true
        url-pattern: /druid/*
        reset-enable: true
        login-username: test
        login-password: 123456
      filter:
        slf4j:
          enabled: true
          statement-create-after-log-enabled: false
          statement-log-enabled: false
          statement-executable-sql-log-enable: true
          statement-log-error-enabled: true
          result-set-log-enabled: false

the end


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK