zl程序教程

您现在的位置是:首页 >  Java

当前栏目

如何关掉Parsed mapper file日志打印

2023-02-18 16:28:37 时间

时间一直走,没有尽头,只有路口。——《摆渡人》

先说结论:

yml配置里的mybatis-plus配置

log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

改为

log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl

yml配置里新增一条

logging:
  level:
    root: DEBUG
    com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean: INFO

完整配置放在最后结尾处

为什么这样配?你是如何找到的?

首先我们搜索Parsed mapper file(因为这个日志大概率是硬编码存在于源码之中的,除非是做了日志本地化,会在配置文件里)

这里排除掉一个纯依赖模块,一个注解模块,挨个到com包搜索,在最后一个extension模块搜到了

进到源码,果然是它打印的,那我们将这个类的日志级别设为INFO应该就搞定了,但是没有生效

我们打个断点,看看什么情况,等断点停到日志这里,我们按下F7

点这个亮着的debug

可以看到这里逻辑:

如果是debug等级,就进行日志打印

我们继续按下F7深入,发现问题了。。此处使用的StdOutImpl是没有进行日志等级管理的

那找到问题了,我们可以换一个日志框架打印

将原来的

log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

改为

log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl

这样我们上面配置的

com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean: INFO

最终测试效果如下:

完整配置如下:

spring:
  datasource:
    driver-class-name: org.h2.Driver
    schema: classpath:schema.sql
    data: classpath:data.sql
    url: jdbc:h2:mem:test

logging:
  level:
    root: DEBUG
    com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean: INFO
mybatis-plus:
  mapper-locations:
    - classpath:mapper/*.xml
  configuration:
    log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl

示例代码仓库地址(可以的话点个star):https://gitee.com/VampireAchao/stream-query.git