我们在使用 Spring5 的过程中会出现这样的现像,就是 Spring5 内部代码打印的日志和我们自己的业务代码打印日志使用的不是统一日志实现,尤其是在项目启动的时候,Spring5 的内部日志使用的是 log4j2,但是业务代码打印使用的可能是 logback ,log4j1 或者 其他日志实现
一、Spring4 采用日志是 commons-logging
二、Spring5 内部日志
1.Spring5 内部日志原理
Spring5 自己在 commons-logging 基础上进行修改,将修改完的代码单独封装为一个 jar 包,Spring5 内部就使用这个jar,我们可以注意一下当我们导入 Spring5 的相关 jar 包的时候,maven会为我们自动依赖一个 spring-jcl 的jar包,这就是那个jar包。
可以看出这这两 jar 包的包名都起的一模一样,只是LogFactory的实现是Spring5自己实现的。
2. Spring5 日志关键源码分析
org.apache.commons.logging.LogFactory#getLog(java.lang.String)
/**
* Convenience method to return a named logger.
* @param name logical name of the <code>Log</code> instance to be returned
*/
public static Log getLog(String name) {
return LogAdapter.createLog(name);
}
org.apache.commons.logging.LogAdapter#createLog
private static final String LOG4J_SPI =
"org.apache.logging.log4j.spi.ExtendedLogger";
private static final String LOG4J_SLF4J_PROVIDER =
"org.apache.logging.slf4j.SLF4JProvider";
private static final String SLF4J_SPI = "org.slf4j.spi.LocationAwareLogger";
private static final String SLF4J_API = "org.slf4j.Logger";
private static final LogApi logApi;
static {
//LOG4J_SPI = "org.apache.logging.log4j.spi.ExtendedLogger"在Log4j2包log4j-api中
//LOG4J_SLF4J_PROVIDER =
// "org.apache.logging.slf4j.SLF4JProvider"在Log4j2桥接器包log4j-to-slf4j中
//SLF4J_SPI = "org.slf4j.spi.LocationAwareLogger" 在slf4j包 slf4j-api 中
//SLF4J_API = "org.slf4j.Logger" 在slf4j包 slf4j-api 中
//判断是否导入了 log4j2包 log4j-api
if (isPresent(LOG4J_SPI)) {
if (
//判断是否导入了 Log4j2 桥接器包log4j-to-slf4j
isPresent(LOG4J_SLF4J_PROVIDER) &&
//判断是否导入了 slf4j包 slf4j-api
isPresent(SLF4J_SPI)
) {
// log4j-to-slf4j bridge -> we'll rather go with the SLF4J SPI;
// however, we still prefer Log4j over the plain SLF4J API since
// the latter does not have location awareness support.
logApi = LogApi.SLF4J_LAL;
}
else {
// Use Log4j 2.x directly, including location awareness support
logApi = LogApi.LOG4J;
}
}
else if (isPresent(SLF4J_SPI)) {
// Full SLF4J SPI including location awareness support
logApi = LogApi.SLF4J_LAL;
}
else if (isPresent(SLF4J_API)) {
// Minimal SLF4J API without location awareness support
logApi = LogApi.SLF4J;
}
else {
// java.util.logging as default
logApi = LogApi.JUL;
}
}
// LOG4J 采用 log4j2的 日志实现
// SLF4J_LAL 采用 slf4j Slf4jLocationAwareLog Full SLF4J SPI
//including location awareness support
// SLF4J 采用 slf4j Slf4jLog Minimal SLF4J API without
//location awareness support
private enum LogApi {LOG4J, SLF4J_LAL, SLF4J, JUL}
public static Log createLog(String name) {
switch (logApi) {
case LOG4J:
return Log4jAdapter.createLog(name);
case SLF4J_LAL:
return Slf4jAdapter.createLocationAwareLog(name);
case SLF4J:
return Slf4jAdapter.createLog(name);
default:
// Defensively use lazy-initializing adapter class here as well since the
// java.logging module is not present by default on JDK 9. We are requiring
// its presence if neither Log4j nor SLF4J is available; however, in the
// case of Log4j or SLF4J, we are trying to prevent early initialization
// of the JavaUtilLog adapter - e.g. by a JVM in debug mode - when eagerly
// trying to parse the bytecode for all the cases of this switch clause.
return JavaUtilAdapter.createLog(name);
}
}