您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關怎么在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中設置默認日志框架有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。