最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot默認日志框架選擇源碼解析(推薦)

 更新時間:2021年03月17日 11:01:21   作者:dengyouhua  
這篇文章主要介紹了springboot默認日志框架選擇源碼解析(推薦),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

背景:

今天新生成一個springboot項目,然而啟動日志,還有mybatis的詳細日志無法打印出來,自寫程序中打印的日志可以輸出;網(wǎng)上找了很多資料,都沒法解決問題;于是決定跟一下源碼,弄清springboot日志相關(guān)的邏輯。

環(huán)境配置: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行注冊的監(jiān)控事件會被ApplicationStartingEvent觸發(fā);如下代碼所示,會調(diào)用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函數(shù)的內(nèi)容:首先會從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是一個接口,它的實現(xiàn)類在spring-boot-start有4個,其中3個是在內(nèi)部內(nèi)類實現(xiàn),DelegatingLoggingSystemFactory(JavaLoggingSystem,Log4J2LoggingSystem,LogbackLoggingSystem,內(nèi)部類實現(xiàn))。上面SYSTEM_FACTORY的實現(xiàn)就是DelegatingLoggingSystemFactory這個類,如下代碼中delegates的值為JavaLoggingSystem,Log4J2LoggingSystem,LogbackLoggingSystem;三個類具體的加載邏輯在SpringFactoriesLoader.loadFactories函數(shù)中,最終返回的loggingSystem就是前面函數(shù)返回列表中的第一個;SpringFactoriesLoader.loadFactories 才是決定springboot默認會使用哪個日志組件關(guān)鍵:該類是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;
  }
 
}

總結(jié):雖然springboot會加載JavaLoggingSystem,Log4J2LoggingSystem,LogbackLoggingSystem三個日志實現(xiàn)類,但在選擇時,還是會使用LogbackLoggingSystem作為它的日志框架

到此這篇關(guān)于springboot默認日志框架選擇源碼解析的文章就介紹到這了,更多相關(guān)springboot日志框架內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java Swing中的表格(JTable)和樹(JTree)組件使用實例

    Java Swing中的表格(JTable)和樹(JTree)組件使用實例

    這篇文章主要介紹了Java Swing中的表格(JTable)和樹(JTree)組件使用實例,本文同時講解了表格和樹的基本概念、常用方法、代碼實例,需要的朋友可以參考下
    2014-10-10
  • POI讀取excel簡介_動力節(jié)點Java學院整理

    POI讀取excel簡介_動力節(jié)點Java學院整理

    這篇文章主要介紹了POI讀取excel簡介,詳細的介紹了什么是Apache POI和組件,有興趣的可以了解了解一下
    2017-08-08
  • java調(diào)用接口返回亂碼問題及解決

    java調(diào)用接口返回亂碼問題及解決

    這篇文章主要介紹了java調(diào)用接口返回亂碼問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • java中判斷字段真實長度的實例(中文2個字符,英文1個字符)

    java中判斷字段真實長度的實例(中文2個字符,英文1個字符)

    下面小編就為大家?guī)硪黄猨ava中判斷字段真實長度的實例(中文2個字符,英文1個字符)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • Springboot 集成 SocketIO的示例代碼

    Springboot 集成 SocketIO的示例代碼

    Socket.IO是實現(xiàn)瀏覽器與服務器之間實時、雙向和基于事件的通信的工具庫,本文主要介紹了Springboot 集成 SocketIO的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-10-10
  • 半小時實現(xiàn)Java手擼網(wǎng)絡爬蟲框架(附完整源碼)

    半小時實現(xiàn)Java手擼網(wǎng)絡爬蟲框架(附完整源碼)

    最近在做一個搜索相關(guān)的項目,需要爬取網(wǎng)絡上的一些鏈接存儲到索引庫中,自己寫了一個簡單的網(wǎng)絡爬蟲,感興趣的可以了解一下
    2021-06-06
  • Quartz與Spring集成的兩種方法示例

    Quartz與Spring集成的兩種方法示例

    這篇文章主要為大家介紹了Quartz與Spring集成的兩種方法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪
    2022-04-04
  • Java 容器類源碼詳解 Set

    Java 容器類源碼詳解 Set

    這篇文章主要介紹了Java 容器類源碼詳解 Set,Set 表示由無重復對象組成的集合,也是集合框架中重要的一種集合類型,直接擴展自 Collection 接口。,需要的朋友可以參考下
    2019-06-06
  • SpringBoot項目打成war和jar的區(qū)別說明

    SpringBoot項目打成war和jar的區(qū)別說明

    這篇文章主要介紹了SpringBoot項目打成war和jar的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Springboot實現(xiàn)動態(tài)定時任務流程詳解

    Springboot實現(xiàn)動態(tài)定時任務流程詳解

    通過重寫SchedulingConfigurer方法實現(xiàn)對定時任務的操作,單次執(zhí)行、停止、啟動三個主要的基本功能,動態(tài)的從數(shù)據(jù)庫中獲取配置的定時任務cron信息,通過反射的方式靈活定位到具體的類與方法中
    2022-09-09

最新評論

鄂伦春自治旗| 涿鹿县| 西城区| 商丘市| 泗洪县| 荥经县| 邹城市| 林甸县| 夏河县| 长乐市| 西林县| 乳源| 永丰县| 封开县| 威信县| 河北区| 新闻| 新和县| 岫岩| 梁河县| 都兰县| 堆龙德庆县| 汽车| 安新县| 惠来县| 西乌珠穆沁旗| 桃江县| 张家界市| 庆城县| 福泉市| 宝应县| 黔西县| 柳河县| 英德市| 济阳县| 犍为县| 林甸县| 松桃| 阿尔山市| 云龙县| 米泉市|