23.SpringBoot学习笔记–SpringBoot日志关系
Spring Boot 底层中关于日志框架的依赖关系如下图。
Spring Boot 使用 spring-boot-starter-logging 完成日志功能
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<version>2.3.0.RELEASE</version>
<scope>compile</scope>
</dependency>
由上面关于日志框架的依赖关系的图可知,Spring Boot 底层使用 SLF4J + Logback 的方式进行日志记录。Spring Boot 也把其他的日志框架都统一为 SLF4J。
下面是 spring-jcl-5.2.6.RELEASE.jar 包的部分目录结构
可以发现报名与 Apache Commons Logging 日志框架的包名一致,但是查看 LogFactory 类中的部分代码:
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 {
if (isPresent(LOG4J_SPI)) {
if (isPresent(LOG4J_SLF4J_PROVIDER) && 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;
}
}
会发现 Apache Commons Logging 日志框架被统一为 SLF4J。
在 spring-boot-starter-2.3.0.RELEASE.pom 文件中发现
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<scope>compile</scope>
</dependency>
并没有排除 Apache Commons Logging 日志框架,这是因为从 Spring 5 开始,底层日志系统依赖的是 spring-jcl,和传统的 JCL (common-logging.jar) 有区别,首先是尝试加载 org.apache.logging.log4j.spi.ExtendedLogger (Log4j API),然后尝试加载 org.slf4j.spi.LocationAwareLogger (SLF4J SPI),再次就是尝试org.slf4j.Logger (SLF4J API),如果还是没有,就加载 JUL。