springBoot中info等级通过druid打印sql

我的springBoot是使用logback来输出日志的.

application.yml中log相关配置如下:

logging:
  pattern:
    console: "%d{yyyy-MM-dd HH:mm:ss.SSS} %clr(%5p) %clr([%15.15t]){cyan} %clr(%X{__traceId}){magenta} %clr(%-40.40logger{39}){blue} %clr(:) %m%n"
    file: "%d{yyyy-MM-dd HH:mm:ss.SSS} %5p [%t] %X{__traceId} %-40.40logger{39} : %m%n"
  level:
    com.xxx.xxx.xxx.repository.mybatis.mapper: debug   #设置mapper.java包为debug输出
  file: c:/tmp/xxx-xxx-web/log.log
  file.max-history: 30

如果项目日志等级为info时是不会打印sql语句的(在debug等级下会打印),如果想要在info等级下打印sql需要在application.yml中的

druid配置部分增加以下配置:

druid:
      filter:
        commons-log:
          #data-source-log-enabled: true
          statement-log-enabled: true
          statement-executable-sql-log-enable: true

通过以上配置即可通过druid在日志等级为info的情况下打印sql;

 

 

在Spring Boot项目,使用Druid作为数据源管理库可以帮助你监控SQL性能,包括执行时间、查询频率等,这对于保证数据库系统的健康至关重要。以下是如何在Spring Boot集成Druid并设置SQL健康检查的基本步骤: 1. 添加依赖:首先,在你的项目的pom.xml文件添加Druid的依赖: ```xml <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.x.y</version> <!-- 根据实际版本替换 --> </dependency> ``` 2. 配置数据源:在application.properties或application.yml文件配置Druid数据源,例如: ```properties spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=root spring.datasource.password=your_password spring.datasource.max-active=50 # ... 其他Druid配置选项,如初始化大小、最小空闲等 ``` 3. 监控配置:启用Druid的监控功能,可以打开web界面查看实时数据。在application.properties添加: ```properties spring.jmx.enabled=true spring.application.admin.enabled=true ``` 然后访问`http://localhost:8080/druid/`即可看到Druid的Web页面,其包括SQL监控。 4. SQL健康检查:Druid本身并未提供直接的SQL健康检查功能,但你可以通过观察SQL执行时间、错误率、频繁运行的查询等指标来判断SQL是否存在问题。例如,你可以设置阈值,如果某个SQL的执行时间超过预设值,就认为这个查询可能是慢查询。 为了定制更高级的健康检查,你还可以考虑使用第三方库,如HikariCP Health Check Adapter,或者自定义监控任务定期检查数据库性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值