【无标题】

Springboot默认日志框架选择源码解析

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> delegates;

/**

  • Create a new {@link DelegatingLoggingSystemFactory} instance.
  • @param delegates a function that provides the delegates
    */
    DelegatingLoggingSystemFactory(Function<ClassLoader, List> delegates) {
    this.delegates = delegates;
    }

@Override
public LoggingSystem getLoggingSystem(ClassLoader classLoader) {
List 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作为它的日志框架

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孤独的深山老人

您的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值