首先看下日誌的啓動流程,說一下前提,日誌啓動是基於spring事件機制,包括dubbo服務導出的啓動也是基於spring事件機制
我們看看spring事件監聽器的定義
/*
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.context;
import java.util.EventListener;
/**
* Interface to be implemented by application event listeners.
* Based on the standard {@code java.util.EventListener} interface
* for the Observer design pattern.
*
* <p>**As of Spring 3.0, an ApplicationListener can generically declare the event type
* that it is interested in. When registered with a Spring ApplicationContext, events
* will be filtered accordingly, with the listener getting invoked for matching event
* objects only.**
*
* @author Rod Johnson
* @author Juergen Hoeller
* @param <E> the specific ApplicationEvent subclass to listen to
* @see org.springframework.context.event.ApplicationEventMulticaster
*/
@FunctionalInterface
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
/**
* Handle an application event.
* @param event the event to respond to
*/
void onApplicationEvent(E event);
}
圖片截圖不全,我們看看源碼中的註釋,就可知道了。我們聲明一個事件類型,spring啓動完成後,會進行分發事件,我們匹配自己定義的事件類型。接下來看看啓動流程,當然是關於日誌的加載流程
日誌系統類有個靜態代碼塊,當類加載的時候,靜態代碼塊也會被加載,靜態代碼塊如下
題外話,判斷一個對象是否是同一個對象?1.由同一個classloader加載,並且是一個同類名。倆個classloader加載一個類,在虛擬機內部會認爲是倆個類。
綜上所述,是加載日誌組件的流程。接下來我們自定義個日誌配置文件,然後看看我們的自定義的xml文件是否生效,以及模擬控制檯不打印日誌的情況
首先演示我們的配置是生效的
接下來把依賴日誌文件更改成log4j2,pom文件以及xml文件如下
xml文件如下
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="error">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
先來我們來看配置是否生效
說明配置已經生效。看看打印日誌的模式樣式是否與原來有區別
現在模擬不打印日誌的情況,log4j2基礎組件截圖發下
不打印情況下,主要是修改log4j2的配置文件,如下圖
綜上所述,我們知道因爲整體項目使用maven依賴,都是在類路徑下,可能再引用其他模塊情況下,覆蓋了我們項目的配置,配置文件中沒有輸出到控制檯,所以出現了不打印的情況。個人能力有限,表述不清請指正。