springboot默認(rèn)日志框架選擇源碼解析(推薦)
背景:
今天新生成一個(gè)springboot項(xiàng)目,然而啟動(dòng)日志,還有mybatis的詳細(xì)日志無(wú)法打印出來(lái),自寫程序中打印的日志可以輸出;網(wǎng)上找了很多資料,都沒(méi)法解決問(wèn)題;于是決定跟一下源碼,弄清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 默認(rèn)日志使用的是logback(引入spring-boot-starter包后,就自動(dòng)引入了logback-core,logback-classic兩個(gè)包,當(dāng)然還有slf4j的包),當(dāng)springboot啟動(dòng)時(shí),org.springframework.boot.context.logging.LoggingApplicationListener,該類211行注冊(cè)的監(jiān)控事件會(huì)被ApplicationStartingEvent觸發(fā);如下代碼所示,會(huì)調(diào)用onApplicationStartingEvent初始化loggingSystem,而使用哪個(gè)日志組件,就要看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)容:首先會(huì)從system.getProperty中獲取className,新生成的項(xiàng)目,取到的這個(gè)值都為空,SYSTEM_PROPERTY是一個(gè)固定值,就是該類的名字;那么loggingSystem的值就是從SYSTEM_FACTORY.getLoggingSystem(classLoader);獲取到的;接下來(lái)我們得看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是一個(gè)接口,它的實(shí)現(xiàn)類在spring-boot-start有4個(gè),其中3個(gè)是在內(nèi)部?jī)?nèi)類實(shí)現(xiàn),DelegatingLoggingSystemFactory(JavaLoggingSystem,Log4J2LoggingSystem,LogbackLoggingSystem,內(nèi)部類實(shí)現(xiàn))。上面SYSTEM_FACTORY的實(shí)現(xiàn)就是DelegatingLoggingSystemFactory這個(gè)類,如下代碼中delegates的值為JavaLoggingSystem,Log4J2LoggingSystem,LogbackLoggingSystem;三個(gè)類具體的加載邏輯在SpringFactoriesLoader.loadFactories函數(shù)中,最終返回的loggingSystem就是前面函數(shù)返回列表中的第一個(gè);SpringFactoriesLoader.loadFactories 才是決定springboot默認(rèn)會(huì)使用哪個(gè)日志組件關(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會(huì)加載JavaLoggingSystem,Log4J2LoggingSystem,LogbackLoggingSystem三個(gè)日志實(shí)現(xiàn)類,但在選擇時(shí),還是會(huì)使用LogbackLoggingSystem作為它的日志框架
到此這篇關(guān)于springboot默認(rèn)日志框架選擇源碼解析的文章就介紹到這了,更多相關(guān)springboot日志框架內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java 數(shù)據(jù)結(jié)構(gòu)進(jìn)階二叉樹題集上
二叉樹可以簡(jiǎn)單理解為對(duì)于一個(gè)節(jié)點(diǎn)來(lái)說(shuō),最多擁有一個(gè)上級(jí)節(jié)點(diǎn),同時(shí)最多具備左右兩個(gè)下級(jí)節(jié)點(diǎn)的數(shù)據(jù)結(jié)構(gòu)。本文將帶你通過(guò)實(shí)際題目來(lái)熟練掌握2022-04-04
簡(jiǎn)單介紹線性表以及如何實(shí)現(xiàn)雙鏈表
本文先介紹線性表的幾個(gè)基本組成部分:數(shù)組、單向鏈表、雙向鏈表;隨后給出雙向鏈表的C、C++和Java三種語(yǔ)言的實(shí)現(xiàn),需要的朋友可以參考下2015-07-07
servlet之session工作原理簡(jiǎn)介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了servlet之session工作原理簡(jiǎn)介,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07
Sublime Text 打開Java文檔中文亂碼的解決方案
這篇文章主要介紹了Sublime Text 中文亂碼的解決方案,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2020-12-12
java利用pdfbox+poi往pdf插入數(shù)據(jù)
這篇文章主要給大家介紹了關(guān)于java利用pdfbox+poi如何往pdf插入數(shù)據(jù)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02
Java中notify()和notifyAll()的使用區(qū)別
本文主要介紹了Java中notify()和notifyAll()的使用區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),感興趣的小伙伴們可以參考一下2021-06-06

