项目中采用springboot+mybatis 其中mybatis 使用了mybatis plus 插件。日志级别调成info 后发现 mybaits 依然狂打sql 及结果集。
仔细查看日志发现 mybatis 使用的是 StdOutImpl 打印日志。和项目中的slf4j不一样。在logback-spring.xml中配置日志级别自然能不起作用 翻看mybaits 源码发现
Logfactory 有以下六种实现
static {
tryImplementation(LogFactory::useSlf4jLogging);
tryImplementation(LogFactory::useCommonsLogging);
tryImplementation(LogFactory::useLog4J2Logging);
tryImplementation(LogFactory::useLog4JLogging);
tryImplementation(LogFactory::useJdkLogging);
tryImplementation(LogFactory::useNoLogging);
}
通过 setImplementation 方法来选择不同的日志框架
private static void setImplementation(Class<? extends Log> implClass) {
try {
Constructor<? extends Log> candidate = implClass.getConstructor(String.class);
Log log = (Log)candidate.newInstance(LogFactory.class.getName());
if (log.isDebugEnabled()) {
log.debug("Logging initialized using '" + implClass + "' adapter.");
}
logConstructor = candidate;
} catch (Throwable var3) {
throw new LogException("Error setting Log implementation. Cause: " + var3, var3);
}
}
通过debug 发现第一次确实是使用加载了slf4j 并且日志级别也和xml里配置的一样。继续debug 发现该方法还会被加载一次就是mybatis 加载配置文件的时候 会被改成 StdOutImpl。所以要更改属性的配置。无意中发现这个属性:
mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
可能mybaits plus 做了默认的设置这个没有细查。配置上之后mybatis 就采用slf4j打印日志了。同时可以更改日志级别来控制sql的打印了。问题解决