springboot默认日志框架选择源码解析(推荐)
作者:dengyouhua 发布时间:2021-06-19 08:02:51
背景:
今天新生成一个springboot项目,然而启动日志,还有mybatis的详细日志无法打印出来,自写程序中打印的日志可以输出;网上找了很多资料,都没法解决问题;于是决定跟一下源码,弄清springboot日志相关的逻辑。
环境配置:macbook; intellij idea community edition 2020.03 ; gradle 6.8.3 jdk1.8 ;
gradle引用包如下:
dependencies {
compile "com.alibaba:fastjson:1.2.75"
compile "mysql:mysql-connector-java"
//spring
compile("org.springframework.boot:spring-boot-starter")
compile("org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.4")
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-actuator")
//lombok
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
//test
testCompile('org.springframework.boot:spring-boot-starter-test')
testImplementation 'io.projectreactor:reactor-test'
}
springboot 默认日志使用的是logback(引入spring-boot-starter包后,就自动引入了logback-core,logback-classic两个包,当然还有slf4j的包),当springboot启动时,org.springframework.boot.context.logging.LoggingApplicationListener,该类211行注册的监控事件会被ApplicationStartingEvent触发;如下代码所示,会调用onApplicationStartingEvent初始化loggingSystem,而使用哪个日志组件,就要看loggingSystem初始化的值了
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ApplicationStartingEvent) {
onApplicationStartingEvent((ApplicationStartingEvent) event);
}
else if (event instanceof ApplicationEnvironmentPreparedEvent) {
onApplicationEnvironmentPreparedEvent((ApplicationEnvironmentPreparedEvent) event);
}
else if (event instanceof ApplicationPreparedEvent) {
onApplicationPreparedEvent((ApplicationPreparedEvent) event);
}
else if (event instanceof ContextClosedEvent
&& ((ContextClosedEvent) event).getApplicationContext().getParent() == null) {
onContextClosedEvent();
}
else if (event instanceof ApplicationFailedEvent) {
onApplicationFailedEvent();
}
}
private void onApplicationStartingEvent(ApplicationStartingEvent event) {
this.loggingSystem = LoggingSystem.get(event.getSpringApplication().getClassLoader());
this.loggingSystem.beforeInitialize();
}
如下图是org.springframework.boot.logging.LoggingSystem类里面get函数的内容:首先会从system.getProperty中获取className,新生成的项目,取到的这个值都为空,SYSTEM_PROPERTY是一个固定值,就是该类的名字;那么loggingSystem的值就是从SYSTEM_FACTORY.getLoggingSystem(classLoader);获取到的;接下来我们得看LoggingSystemFactory.fromSpringFactories.getLoggingSystem取的值是什么了;
public static final String SYSTEM_PROPERTY = LoggingSystem.class.getName();
private static final LoggingSystemFactory SYSTEM_FACTORY = LoggingSystemFactory.fromSpringFactories();
public static LoggingSystem get(ClassLoader classLoader) {
String loggingSystemClassName = System.getProperty(SYSTEM_PROPERTY);
if (StringUtils.hasLength(loggingSystemClassName)) {
if (NONE.equals(loggingSystemClassName)) {
return new NoOpLoggingSystem();
}
return get(classLoader, loggingSystemClassName);
}
LoggingSystem loggingSystem = SYSTEM_FACTORY.getLoggingSystem(classLoader);
Assert.state(loggingSystem != null, "No suitable logging system located");
return loggingSystem;
}
private static LoggingSystem get(ClassLoader classLoader, String loggingSystemClassName) {
try {
Class<?> systemClass = ClassUtils.forName(loggingSystemClassName, classLoader);
Constructor<?> constructor = systemClass.getDeclaredConstructor(ClassLoader.class);
constructor.setAccessible(true);
return (LoggingSystem) constructor.newInstance(classLoader);
}
catch (Exception ex) {
throw new IllegalStateException(ex);
}
}
LoggingSystemFactory是一个接口,它的实现类在spring-boot-start有4个,其中3个是在内部内类实现,DelegatingLoggingSystemFactory(JavaLoggingSystem,Log4J2LoggingSystem,LogbackLoggingSystem,内部类实现)。上面SYSTEM_FACTORY的实现就是DelegatingLoggingSystemFactory这个类,如下代码中delegates的值为JavaLoggingSystem,Log4J2LoggingSystem,LogbackLoggingSystem;三个类具体的加载逻辑在SpringFactoriesLoader.loadFactories函数中,最终返回的loggingSystem就是前面函数返回列表中的第一个;SpringFactoriesLoader.loadFactories 才是决定springboot默认会使用哪个日志组件关键:该类是spring的核心组件类,在spring-core包中,org.springframework.core.io.support.SpringFactoriesLoader;loggingSystem的值=LogbackLoggingSystem
public interface LoggingSystemFactory {
/**
* Return a logging system implementation or {@code null} if no logging system is
* available.
* @param classLoader the class loader to use
* @return a logging system
*/
LoggingSystem getLoggingSystem(ClassLoader classLoader);
/**
* Return a {@link LoggingSystemFactory} backed by {@code spring.factories}.
* @return a {@link LoggingSystemFactory} instance
*/
static LoggingSystemFactory fromSpringFactories() {
return new DelegatingLoggingSystemFactory(
(classLoader) -> SpringFactoriesLoader.loadFactories(LoggingSystemFactory.class, classLoader));
}
}
class DelegatingLoggingSystemFactory implements LoggingSystemFactory {
private final Function<ClassLoader, List<LoggingSystemFactory>> delegates;
/**
* Create a new {@link DelegatingLoggingSystemFactory} instance.
* @param delegates a function that provides the delegates
*/
DelegatingLoggingSystemFactory(Function<ClassLoader, List<LoggingSystemFactory>> delegates) {
this.delegates = delegates;
}
@Override
public LoggingSystem getLoggingSystem(ClassLoader classLoader) {
List<LoggingSystemFactory> delegates = (this.delegates != null) ? this.delegates.apply(classLoader) : null;
if (delegates != null) {
for (LoggingSystemFactory delegate : delegates) {
LoggingSystem loggingSystem = delegate.getLoggingSystem(classLoader);
if (loggingSystem != null) {
return loggingSystem;
}
}
}
return null;
}
}
总结:虽然springboot会加载JavaLoggingSystem,Log4J2LoggingSystem,LogbackLoggingSystem三个日志实现类,但在选择时,还是会使用LogbackLoggingSystem作为它的日志框架
来源:https://www.cnblogs.com/deng-1024/p/14525895.html


猜你喜欢
- 泛型List<T>中有一个比较列表是否已包含对象的方法Contains<T>(),今天在网上搜了一个用法,记录下来,
- AndroidProgressLayout实现为界面添加圆形进度条。调用setprogress()方法显示和隐藏进度条在Android的开发
- 继承反应了类和类之间的关系。世界上很多事物都是有共性的,共性的那一部分我们就抽象为基类,用于派生其它类,这样提高了代码的复用性,使得代码的结
- 最近部分采用了TDD的方法来开发一个模块,小有收获特此总结一下:1. TDD的基本原则TDD的最核心思想就是先明确需求,且用代码的方式量化,
- 四大函数式接口新时代的程序员:lambda 表达式,链式编程,函数式接口,Stream 流式计算函数式接口: 只有一个方法的接口@Funct
- springboot集成swagger3swagger3的springboot启动器jar包<!-- https://mvnrepos
- springboot静态资源加载顺序优先级看springboot源码里面springboot静态资源加载规则我们经常会使用springboo
- 前言用户注册功能是每一个系统的入口门面功能,很多人可能会以为很简单,不就是一个简单的CRUD吗?其实不然,要把前后端功能都做出来,页面跳转也
- 这里主要是总结一下如何监听有未接来电的问题 1.1 使用广播 * BrocastReceiver实现思路 : 静态注册监听and
- 本文实例讲述了C#图像处理之图像平移的方法。分享给大家供大家参考。具体如下://定义图像平移函数private static Bitmap
- Java调用Linux系统命令有时候,我们在使用Java做一些操作时,可能性能上并不能达到我们满意的效果,就拿最近工作中的遇到的一个场景来说
- spring容器初始化Bean操作在某些情况下,Spring容器在初始化Bean的时候,希望在初始化bean前和销毁bean前进行一些资源的
- 职责链模式简介及UML职责链也叫责任链,他是一种行为型模式,它为请求创建了一个接收请求者对象的链,并将请求沿着这条链传递到目标对象去处理。该
- 群里有人问,怎样直接清空堆里的string值。有人建议直接用Dispose()方法;Dispose()销毁了对象,是一种垃圾回收机制。(这里
- 虽然listview是过去式,但由于项目中还是有用listview,百度一番都是scrollview中的悬浮bar,没有看到有listvie
- Android自带的跑马灯效果不太好控制,还必须要满足条件才能有效果,而且速度不受控制。前面我的博客中有一篇就是用Android自带的跑马灯
- SpringBoot 中使用activeMq的步骤:1、pom中引入jar包: <dependency>  
- 1、不知道为啥process.StartInfo.Arguments = "/c" + "start D:/T
- android去掉滑动到顶部和底部的阴影<ListViewandroid:id="@+id/listView"an
- 原文是 java ,现在将它翻译成 C# ,并对代码重新编排整理,博主是一个今年刚出来的应届毕业生,不足之处请多多包涵。根据银行卡号判断所属