99
1010- 日志级别: ` org.springframework.boot.logging.LogLevel `
1111
12- ```java
13- public enum LogLevel {
14- TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF
15- }
16- ```
12+ ``` java
13+ public enum LogLevel {
14+ TRACE , DEBUG , INFO , WARN , ERROR , FATAL , OFF
15+ }
16+ ```
1717
1818## Java 日志实现
1919
2020- ` org.springframework.boot.logging.java.JavaLoggingSystem `
2121
22- 
23-
24- ```java
25- static {
26- // KEY : springBoot 定义的日志级别, value: jdk 定义的日志级别
27- LEVELS.map(LogLevel.TRACE, Level.FINEST);
28- LEVELS.map(LogLevel.DEBUG, Level.FINE);
29- LEVELS.map(LogLevel.INFO, Level.INFO);
30- LEVELS.map(LogLevel.WARN, Level.WARNING);
31- LEVELS.map(LogLevel.ERROR, Level.SEVERE);
32- LEVELS.map(LogLevel.FATAL, Level.SEVERE);
33- LEVELS.map(LogLevel.OFF, Level.OFF);
34- }
35- ```
22+ ![ image-20200323144523848] ( ../../images/SpringBoot/image-20200323144523848.png )
23+
24+ ``` java
25+ static {
26+ // KEY : springBoot 定义的日志级别, value: jdk 定义的日志级别
27+ LEVELS . map(LogLevel . TRACE , Level . FINEST );
28+ LEVELS . map(LogLevel . DEBUG , Level . FINE );
29+ LEVELS . map(LogLevel . INFO , Level . INFO );
30+ LEVELS . map(LogLevel . WARN , Level . WARNING );
31+ LEVELS . map(LogLevel . ERROR , Level . SEVERE );
32+ LEVELS . map(LogLevel . FATAL , Level . SEVERE );
33+ LEVELS . map(LogLevel . OFF , Level . OFF );
34+ }
35+ ```
3636
3737- LEVELS 对象
3838
39- ```java
40- protected static class LogLevels<T> {
41- /**
42- * key : SpringBoot 中定义的日志级别, value: 其他日志框架的日志级别
43- */
44- private final Map<LogLevel, T> systemToNative;
45- /**
46- * key : 其他日志框架的日志级别 , value: springBoot 中定义中定义的日志级别
47- */
48- private final Map<T, LogLevel> nativeToSystem;
49- }
50- ```
39+ ``` java
40+ protected static class LogLevels <T> {
41+ /**
42+ * key : SpringBoot 中定义的日志级别, value: 其他日志框架的日志级别
43+ */
44+ private final Map<LogLevel , T > systemToNative;
45+ /**
46+ * key : 其他日志框架的日志级别 , value: springBoot 中定义中定义的日志级别
47+ */
48+ private final Map<T , LogLevel > nativeToSystem;
49+ }
50+ ```
5151
5252## LoggingSystem
5353
5656
5757- 一个 map 对象: ` SYSTEMS `
5858
59- ```java
60- /**
61- * key: 第三方日志框架的类 value: springBoot 中的处理类
62- */
63- private static final Map<String, String> SYSTEMS;
64-
65- static {
66- Map<String, String> systems = new LinkedHashMap<>();
67- systems.put("ch.qos.logback.core.Appender", "org.springframework.boot.logging.logback.LogbackLoggingSystem");
68- systems.put("org.apache.logging.log4j.core.impl.Log4jContextFactory",
69- "org.springframework.boot.logging.log4j2.Log4J2LoggingSystem");
70- systems.put("java.util.logging.LogManager", "org.springframework.boot.logging.java.JavaLoggingSystem");
71- SYSTEMS = Collections.unmodifiableMap(systems);
72- }
73- ```
59+ ``` java
60+ /**
61+ * key: 第三方日志框架的类 value: springBoot 中的处理类
62+ */
63+ private static final Map<String , String > SYSTEMS ;
64+
65+ static {
66+ Map<String , String > systems = new LinkedHashMap<> ();
67+ systems. put(" ch.qos.logback.core.Appender" , " org.springframework.boot.logging.logback.LogbackLoggingSystem" );
68+ systems. put(" org.apache.logging.log4j.core.impl.Log4jContextFactory" ,
69+ " org.springframework.boot.logging.log4j2.Log4J2LoggingSystem" );
70+ systems. put(" java.util.logging.LogManager" , " org.springframework.boot.logging.java.JavaLoggingSystem" );
71+ SYSTEMS = Collections . unmodifiableMap(systems);
72+ }
73+ ```
7474
7575- 各个抽象方法
7676
77- | 方法名称 | 作用 |
78- | ----------------------- | ---------------------------------- |
79- | beforeInitialize | 初始化之前调用,目的是减少日志输出 |
80- | initialize | 初始化日志 |
81- | cleanUp | 清除日志 |
82- | getShutdownHandler | |
83- | getSupportedLogLevels | 获取支持的日志级别 |
84- | setLogLevel | 设置日志级别 |
85- | getLoggerConfigurations | 获取日志配置 |
77+ | 方法名称 | 作用 |
78+ | ----------------------- | ---------------------------------- |
79+ | beforeInitialize | 初始化之前调用,目的是减少日志输出 |
80+ | initialize | 初始化日志 |
81+ | cleanUp | 清除日志 |
82+ | getShutdownHandler | |
83+ | getSupportedLogLevels | 获取支持的日志级别 |
84+ | setLogLevel | 设置日志级别 |
85+ | getLoggerConfigurations | 获取日志配置 |
8686
8787### get
8888
@@ -136,27 +136,28 @@ private static LoggingSystem get(ClassLoader classLoader, String loggingSystemCl
136136 ![ image-20200323154205484] ( ../../images/SpringBoot/image-20200323154205484.png )
137137
138138- 链路
139- 1. `org.springframework.boot.context.logging.LoggingApplicationListener#onApplicationEvent`
140- 2. `org.springframework.boot.context.logging.LoggingApplicationListener#onApplicationStartingEvent`
141- 3. `org.springframework.boot.logging.LoggingSystem#beforeInitialize`
139+
140+ 1 . ` org.springframework.boot.context.logging.LoggingApplicationListener#onApplicationEvent `
141+ 2 . ` org.springframework.boot.context.logging.LoggingApplicationListener#onApplicationStartingEvent `
142+ 3 . ` org.springframework.boot.logging.LoggingSystem#beforeInitialize `
142143
143144- 因为前文中我们已知对象是:` org.springframework.boot.logging.logback.LogbackLoggingSystem ` 直接看这个类的 ` beforeInitialize ` 方法
144145
145- ```java
146- @Override
147- public void beforeInitialize() {
148- // 日志上下文
149- LoggerContext loggerContext = getLoggerContext();
150- // 是否初始化
151- if (isAlreadyInitialized(loggerContext)) {
152- return;
153- }
154- // 父类方法
155- super.beforeInitialize();
156- // 添加过滤器
157- loggerContext.getTurboFilterList().add(FILTER);
158- }
159- ```
146+ ``` java
147+ @Override
148+ public void beforeInitialize() {
149+ // 日志上下文
150+ LoggerContext loggerContext = getLoggerContext();
151+ // 是否初始化
152+ if (isAlreadyInitialized(loggerContext)) {
153+ return ;
154+ }
155+ // 父类方法
156+ super . beforeInitialize();
157+ // 添加过滤器
158+ loggerContext. getTurboFilterList(). add(FILTER );
159+ }
160+ ```
160161
161162- 初始化之前的的操作完成了初始化方法开始
162163
@@ -286,26 +287,26 @@ private static LoggingSystem get(ClassLoader classLoader, String loggingSystemCl
286287 }
287288 ```
288289
289- ```java
290- @Override
291- public void initialize(LoggingInitializationContext initializationContext, String configLocation, LogFile logFile) {
292- LoggerContext loggerContext = getLoggerContext();
293- // 是否加载过
294- if (isAlreadyInitialized(loggerContext)) {
295- return;
296- }
297- // 日志初始化
298- super.initialize(initializationContext, configLocation, logFile);
299- // 删除 FILTER
300- loggerContext.getTurboFilterList().remove(FILTER);
301- // 初始化标记
302- markAsInitialized(loggerContext);
303- if (StringUtils.hasText(System.getProperty(CONFIGURATION_FILE_PROPERTY))) {
304- getLogger(LogbackLoggingSystem.class.getName()).warn("Ignoring '" + CONFIGURATION_FILE_PROPERTY
305- + "' system property. Please use 'logging.config' instead.");
306- }
307- }
308- ```
290+ ``` java
291+ @Override
292+ public void initialize(LoggingInitializationContext initializationContext, String configLocation, LogFile logFile) {
293+ LoggerContext loggerContext = getLoggerContext();
294+ // 是否加载过
295+ if (isAlreadyInitialized(loggerContext)) {
296+ return ;
297+ }
298+ // 日志初始化
299+ super . initialize(initializationContext, configLocation, logFile);
300+ // 删除 FILTER
301+ loggerContext. getTurboFilterList(). remove(FILTER );
302+ // 初始化标记
303+ markAsInitialized(loggerContext);
304+ if (StringUtils . hasText(System . getProperty(CONFIGURATION_FILE_PROPERTY ))) {
305+ getLogger(LogbackLoggingSystem . class. getName()). warn(" Ignoring '" + CONFIGURATION_FILE_PROPERTY
306+ + " ' system property. Please use 'logging.config' instead." );
307+ }
308+ }
309+ ```
309310
310311- 标记 ` markAsInitialized `
311312
@@ -332,49 +333,49 @@ private static LoggingSystem get(ClassLoader classLoader, String loggingSystemCl
332333
333334- 添加依赖
334335
335- ```XML
336- <dependency>
337- <groupId>org.springframework.boot</groupId>
338- <artifactId>spring-boot-starter-logging</artifactId>
339- <version>${revision}</version>
340- </dependency>
336+ ``` XML
337+ <dependency >
338+ <groupId >org.springframework.boot</groupId >
339+ <artifactId >spring-boot-starter-logging</artifactId >
340+ <version >${revision}</version >
341+ </dependency >
341342
342- ```
343+ ```
343344
344345- 添加配置文件
345346
346- 
347+ ![ image-20200323161442058] ( ../../images/SpringBoot/image-20200323161442058.png )
347348
348- 
349+ ![ image-20200323161522570] ( ../../images/SpringBoot/image-20200323161522570.png )
349350
350351- 此时配置文件地址出现了
351352
352- ```java
353- protected String getSelfInitializationConfig() {
354- // 寻找配置文件
355- return findConfig(getStandardConfigLocations());
356- }
357- ```
353+ ``` java
354+ protected String getSelfInitializationConfig() {
355+ // 寻找配置文件
356+ return findConfig(getStandardConfigLocations());
357+ }
358+ ```
358359
359- ```java
360- @Override
361- protected String[] getStandardConfigLocations() {
362- return new String[] { "logback-test.groovy", "logback-test.xml", "logback.groovy", "logback.xml" };
363- }
360+ ``` java
361+ @Override
362+ protected String [] getStandardConfigLocations() {
363+ return new String [] { " logback-test.groovy" , " logback-test.xml" , " logback.groovy" , " logback.xml" };
364+ }
364365
365- ```
366+ ```
366367
367- ```java
368- private String findConfig(String[] locations) {
369- for (String location : locations) {
370- ClassPathResource resource = new ClassPathResource(location, this.classLoader);
371- if (resource.exists()) {
372- return "classpath:" + location;
373- }
374- }
375- return null;
376- }
377- ```
368+ ``` java
369+ private String findConfig(String [] locations) {
370+ for (String location : locations) {
371+ ClassPathResource resource = new ClassPathResource (location, this . classLoader);
372+ if (resource. exists()) {
373+ return " classpath:" + location;
374+ }
375+ }
376+ return null ;
377+ }
378+ ```
378379
379380- 此时自定义配置文件如何获取的已经明了。
380381
0 commit comments