JUL
java自带的一个日志记录的技术(java.util.logging),默认打印info及以上级别
JCL
commons-logging.jar
本身不记录日志,如果配置了log4j(引入log4j架包),则使用log4j,否则使用JUL
private static final String[] classesToDiscover = {
org.apache.commons.logging.impl.Log4JLogger,
"org.apache.commons.logging.impl.Jdk14Logger",
"org.apache.commons.logging.impl.Jdk13LumberjackLogger",
"org.apache.commons.logging.impl.SimpleLog"
};
slf4j(Simple Logging Facade for Java)
slf4j本身也不记录日志,通过绑定器绑定一个具体的日志框架来完成日志记录:
slf4j-log4j12-1.8.0-beta2.jar
slf4j-jdk14-1.8.0-beta2.jar
slf4j-nop-1.8.0-beta2.jar
slf4j-simple-1.8.0-beta2.jar
slf4j-jcl-1.8.0-beta2.jar
logback-classic-1.0.13.jar (requires logback-core-1.0.13.jar)
另外,可以将其他的日志框架桥接到slf4j上:
jcl-over-slf4j.jar
log4j-over-slf4j
jul-to-slf4j
Spring4
Spring4 当中依赖JCL,采用的日志技术是JCL,引入 commons-logging.jar
会先判断是否引入了log4j,如果引入了则使用log4j,否则使用JUL
Spring5
默认使用的是JUL, 引入 spring-jcl.jar
spring-jcl.jar里面的内容还是commons-logging.jar的内容,只是spring将commons-logging的源码进行了修改
按Log4j 2.x,SLF4J 1.7 SPI,SLF4J 1.7 API,JUL 的顺序使用
如果想使用Log4j,可以引入SLF4J,将SLF4J绑定到Log4j
private static LogApi logApi = LogApi.JUL;
static {
ClassLoader cl = LogFactory.class.getClassLoader();
try {
// Try Log4j 2.x API
cl.loadClass("org.apache.logging.log4j.spi.ExtendedLogger");
logApi = LogApi.LOG4J;
}
catch (ClassNotFoundException ex1) {
try {
// Try SLF4J 1.7 SPI
cl.loadClass("org.slf4j.spi.LocationAwareLogger");
logApi = LogApi.SLF4J_LAL;
}
catch (ClassNotFoundException ex2) {
try {
// Try SLF4J 1.7 API
cl.loadClass("org.slf4j.Logger");
logApi = LogApi.SLF4J;
}
catch (ClassNotFoundException ex3) {
// Keep java.util.logging as default
}
}
}
}
mybatis
mybatis的日志工厂类:org.apache.ibatis.logging.LogFactory会按如下顺序加载日志框架:
SLF4J、JCL、Log4j 2、Log4j、JUL
public static synchronized void useSlf4jLogging() {
setImplementation(org.apache.ibatis.logging.slf4j.Slf4jImpl.class);
}
public static synchronized void useCommonsLogging() {
setImplementation(org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.class);
}
public static synchronized void useLog4J2Logging() {
setImplementation(org.apache.ibatis.logging.log4j2.Log4j2Impl.class);
}
public static synchronized void useLog4JLogging() {
setImplementation(org.apache.ibatis.logging.log4j.Log4jImpl.class);
}
public static synchronized void useJdkLogging() {
setImplementation(org.apache.ibatis.logging.jdk14.Jdk14LoggingImpl.class);
}
public static synchronized void useNoLogging() {
setImplementation(org.apache.ibatis.logging.nologging.NoLoggingImpl.class);
}
如果想让Mybatis使用某种日志框架,可以在调用其他mybatis方法之前调用如下方法:
org.apache.ibatis.logging.LogFactory.useSlf4jLogging();
org.apache.ibatis.logging.LogFactory.useLog4JLogging();
org.apache.ibatis.logging.LogFactory.useLog4J2Logging();
org.apache.ibatis.logging.LogFactory.useJdkLogging();
org.apache.ibatis.logging.LogFactory.useCommonsLogging();
org.apache.ibatis.logging.LogFactory.useStdOutLogging();
想让mybatis使用Log4j,引入了log4j的架包:
如果mybatis和Spring4整合,spring4会引入JCL,JCL判断引入了log4j则使用log4j, LogFactory初始时加载到JCL时,判断存在JCL,会设置为使用JCL,
所以,无需再明确调用org.apache.ibatis.logging.LogFactory.useLog4JLogging();也可以使用log4j
但是,如果mybatis和Spring5整合,Spring5不会引入JCL,而是引入spring自身spring-jcl.jar,按Log4j 2.x,SLF4J 1.7 SPI,SLF4J 1.7 API,JUL 的顺序使用,
需要明确调用org.apache.ibatis.logging.LogFactory.useLog4JLogging();才可以使用log4j,并且调用的地方需要在Spring初始化之前
参考资料:https://shimo.im/docs/kKCgwqgQqHYTeQ8J/read 《spring5通用日志框架》