|
|
@ -15,13 +15,13 @@ public enum LogLevel {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## java 日志实现
|
|
|
|
## Java 日志实现
|
|
|
|
|
|
|
|
|
|
|
|
- `org.springframework.boot.logging.java.JavaLoggingSystem`
|
|
|
|
- `org.springframework.boot.logging.java.JavaLoggingSystem`
|
|
|
|
|
|
|
|
|
|
|
|

|
|
|
|

|
|
|
|
|
|
|
|
|
|
|
|
```JAVA
|
|
|
|
```java
|
|
|
|
static {
|
|
|
|
static {
|
|
|
|
// KEY : springBoot 定义的日志级别, value: jdk 定义的日志级别
|
|
|
|
// KEY : springBoot 定义的日志级别, value: jdk 定义的日志级别
|
|
|
|
LEVELS.map(LogLevel.TRACE, Level.FINEST);
|
|
|
|
LEVELS.map(LogLevel.TRACE, Level.FINEST);
|
|
|
@ -38,12 +38,10 @@ public enum LogLevel {
|
|
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
```java
|
|
|
|
protected static class LogLevels<T> {
|
|
|
|
protected static class LogLevels<T> {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* key : SpringBoot 中定义的日志级别, value: 其他日志框架的日志级别
|
|
|
|
* key : SpringBoot 中定义的日志级别, value: 其他日志框架的日志级别
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private final Map<LogLevel, T> systemToNative;
|
|
|
|
private final Map<LogLevel, T> systemToNative;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* key : 其他日志框架的日志级别 , value: springBoot 中定义中定义的日志级别
|
|
|
|
* key : 其他日志框架的日志级别 , value: springBoot 中定义中定义的日志级别
|
|
|
|
*/
|
|
|
|
*/
|
|
|
@ -58,7 +56,7 @@ public enum LogLevel {
|
|
|
|
|
|
|
|
|
|
|
|
- 一个 map 对象: `SYSTEMS`
|
|
|
|
- 一个 map 对象: `SYSTEMS`
|
|
|
|
|
|
|
|
|
|
|
|
```JAVA
|
|
|
|
```java
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* key: 第三方日志框架的类 value: springBoot 中的处理类
|
|
|
|
* key: 第三方日志框架的类 value: springBoot 中的处理类
|
|
|
|
*/
|
|
|
|
*/
|
|
|
@ -72,7 +70,6 @@ public enum LogLevel {
|
|
|
|
systems.put("java.util.logging.LogManager", "org.springframework.boot.logging.java.JavaLoggingSystem");
|
|
|
|
systems.put("java.util.logging.LogManager", "org.springframework.boot.logging.java.JavaLoggingSystem");
|
|
|
|
SYSTEMS = Collections.unmodifiableMap(systems);
|
|
|
|
SYSTEMS = Collections.unmodifiableMap(systems);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
- 各个抽象方法
|
|
|
|
- 各个抽象方法
|
|
|
@ -143,9 +140,9 @@ public static LoggingSystem get(ClassLoader classLoader) {
|
|
|
|
2. `org.springframework.boot.context.logging.LoggingApplicationListener#onApplicationStartingEvent`
|
|
|
|
2. `org.springframework.boot.context.logging.LoggingApplicationListener#onApplicationStartingEvent`
|
|
|
|
3. `org.springframework.boot.logging.LoggingSystem#beforeInitialize`
|
|
|
|
3. `org.springframework.boot.logging.LoggingSystem#beforeInitialize`
|
|
|
|
|
|
|
|
|
|
|
|
- 因为前文中我们已知对象是:`org.springframework.boot.logging.logback.LogbackLoggingSystem` 直接看这个类的**`beforeInitialize`**方法
|
|
|
|
- 因为前文中我们已知对象是:`org.springframework.boot.logging.logback.LogbackLoggingSystem` 直接看这个类的 `beforeInitialize` 方法
|
|
|
|
|
|
|
|
|
|
|
|
```JAVA
|
|
|
|
```java
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void beforeInitialize() {
|
|
|
|
public void beforeInitialize() {
|
|
|
|
// 日志上下文
|
|
|
|
// 日志上下文
|
|
|
@ -159,7 +156,6 @@ public static LoggingSystem get(ClassLoader classLoader) {
|
|
|
|
// 添加过滤器
|
|
|
|
// 添加过滤器
|
|
|
|
loggerContext.getTurboFilterList().add(FILTER);
|
|
|
|
loggerContext.getTurboFilterList().add(FILTER);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
- 初始化之前的的操作完成了初始化方法开始
|
|
|
|
- 初始化之前的的操作完成了初始化方法开始
|
|
|
@ -168,7 +164,7 @@ public static LoggingSystem get(ClassLoader classLoader) {
|
|
|
|
|
|
|
|
|
|
|
|
- `org.springframework.boot.context.logging.LoggingApplicationListener#onApplicationEnvironmentPreparedEvent`
|
|
|
|
- `org.springframework.boot.context.logging.LoggingApplicationListener#onApplicationEnvironmentPreparedEvent`
|
|
|
|
|
|
|
|
|
|
|
|
```JAVA
|
|
|
|
```java
|
|
|
|
private void onApplicationEnvironmentPreparedEvent(ApplicationEnvironmentPreparedEvent event) {
|
|
|
|
private void onApplicationEnvironmentPreparedEvent(ApplicationEnvironmentPreparedEvent event) {
|
|
|
|
if (this.loggingSystem == null) {
|
|
|
|
if (this.loggingSystem == null) {
|
|
|
|
this.loggingSystem = LoggingSystem.get(event.getSpringApplication().getClassLoader());
|
|
|
|
this.loggingSystem = LoggingSystem.get(event.getSpringApplication().getClassLoader());
|
|
|
@ -180,7 +176,7 @@ public static LoggingSystem get(ClassLoader classLoader) {
|
|
|
|
|
|
|
|
|
|
|
|
- `org.springframework.boot.context.logging.LoggingApplicationListener#initializeSystem`
|
|
|
|
- `org.springframework.boot.context.logging.LoggingApplicationListener#initializeSystem`
|
|
|
|
|
|
|
|
|
|
|
|
```JAVA
|
|
|
|
```java
|
|
|
|
protected void initialize(ConfigurableEnvironment environment, ClassLoader classLoader) {
|
|
|
|
protected void initialize(ConfigurableEnvironment environment, ClassLoader classLoader) {
|
|
|
|
new LoggingSystemProperties(environment).apply();
|
|
|
|
new LoggingSystemProperties(environment).apply();
|
|
|
|
this.logFile = LogFile.get(environment);
|
|
|
|
this.logFile = LogFile.get(environment);
|
|
|
@ -199,7 +195,7 @@ public static LoggingSystem get(ClassLoader classLoader) {
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
```JAVA
|
|
|
|
```java
|
|
|
|
private void initializeSystem(ConfigurableEnvironment environment, LoggingSystem system, LogFile logFile) {
|
|
|
|
private void initializeSystem(ConfigurableEnvironment environment, LoggingSystem system, LogFile logFile) {
|
|
|
|
LoggingInitializationContext initializationContext = new LoggingInitializationContext(environment);
|
|
|
|
LoggingInitializationContext initializationContext = new LoggingInitializationContext(environment);
|
|
|
|
String logConfig = environment.getProperty(CONFIG_PROPERTY);
|
|
|
|
String logConfig = environment.getProperty(CONFIG_PROPERTY);
|
|
|
@ -220,7 +216,6 @@ public static LoggingSystem get(ClassLoader classLoader) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
- `org.springframework.boot.logging.logback.LogbackLoggingSystem#initialize`
|
|
|
|
- `org.springframework.boot.logging.logback.LogbackLoggingSystem#initialize`
|
|
|
@ -246,7 +241,7 @@ public static LoggingSystem get(ClassLoader classLoader) {
|
|
|
|
|
|
|
|
|
|
|
|
- `org.springframework.boot.logging.AbstractLoggingSystem#initializeWithConventions`
|
|
|
|
- `org.springframework.boot.logging.AbstractLoggingSystem#initializeWithConventions`
|
|
|
|
|
|
|
|
|
|
|
|
```JAVA
|
|
|
|
```java
|
|
|
|
private void initializeWithConventions(LoggingInitializationContext initializationContext, LogFile logFile) {
|
|
|
|
private void initializeWithConventions(LoggingInitializationContext initializationContext, LogFile logFile) {
|
|
|
|
String config = getSelfInitializationConfig();
|
|
|
|
String config = getSelfInitializationConfig();
|
|
|
|
if (config != null && logFile == null) {
|
|
|
|
if (config != null && logFile == null) {
|
|
|
@ -264,12 +259,11 @@ public static LoggingSystem get(ClassLoader classLoader) {
|
|
|
|
// 加载默认配置
|
|
|
|
// 加载默认配置
|
|
|
|
loadDefaults(initializationContext, logFile);
|
|
|
|
loadDefaults(initializationContext, logFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
- `org.springframework.boot.logging.logback.LogbackLoggingSystem#loadDefaults`
|
|
|
|
- `org.springframework.boot.logging.logback.LogbackLoggingSystem#loadDefaults`
|
|
|
|
|
|
|
|
|
|
|
|
```JAVA
|
|
|
|
```java
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
protected void loadDefaults(LoggingInitializationContext initializationContext, LogFile logFile) {
|
|
|
|
protected void loadDefaults(LoggingInitializationContext initializationContext, LogFile logFile) {
|
|
|
|
LoggerContext context = getLoggerContext();
|
|
|
|
LoggerContext context = getLoggerContext();
|
|
|
@ -290,10 +284,9 @@ public static LoggingSystem get(ClassLoader classLoader) {
|
|
|
|
new DefaultLogbackConfiguration(initializationContext, logFile).apply(configurator);
|
|
|
|
new DefaultLogbackConfiguration(initializationContext, logFile).apply(configurator);
|
|
|
|
context.setPackagingDataEnabled(true);
|
|
|
|
context.setPackagingDataEnabled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
```JAVA
|
|
|
|
```java
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void initialize(LoggingInitializationContext initializationContext, String configLocation, LogFile logFile) {
|
|
|
|
public void initialize(LoggingInitializationContext initializationContext, String configLocation, LogFile logFile) {
|
|
|
|
LoggerContext loggerContext = getLoggerContext();
|
|
|
|
LoggerContext loggerContext = getLoggerContext();
|
|
|
@ -314,27 +307,25 @@ public static LoggingSystem get(ClassLoader classLoader) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
标记`markAsInitialized`
|
|
|
|
- 标记 `markAsInitialized`
|
|
|
|
|
|
|
|
|
|
|
|
```JAVA
|
|
|
|
```java
|
|
|
|
private void markAsInitialized(LoggerContext loggerContext) {
|
|
|
|
private void markAsInitialized(LoggerContext loggerContext) {
|
|
|
|
loggerContext.putObject(LoggingSystem.class.getName(), new Object());
|
|
|
|
loggerContext.putObject(LoggingSystem.class.getName(), new Object());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
此时日志初始化完成
|
|
|
|
此时日志初始化完成。
|
|
|
|
|
|
|
|
|
|
|
|
### 默认配置文件
|
|
|
|
### 默认配置文件
|
|
|
|
|
|
|
|
|
|
|
|
- `getStandardConfigLocations` 这个方法定义了默认配置文件有哪些
|
|
|
|
- `getStandardConfigLocations` 这个方法定义了默认配置文件有哪些。
|
|
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
```java
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
protected String[] getStandardConfigLocations() {
|
|
|
|
protected String[] getStandardConfigLocations() {
|
|
|
|
return new String[] { "logback-test.groovy", "logback-test.xml", "logback.groovy", "logback.xml" };
|
|
|
|
return new String[] { "logback-test.groovy", "logback-test.xml", "logback.groovy", "logback.xml" };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
- 切回 `org.springframework.boot.logging.AbstractLoggingSystem#initializeWithConventions` 方法
|
|
|
|
- 切回 `org.springframework.boot.logging.AbstractLoggingSystem#initializeWithConventions` 方法
|
|
|
@ -358,15 +349,14 @@ public static LoggingSystem get(ClassLoader classLoader) {
|
|
|
|
|
|
|
|
|
|
|
|
- 此时配置文件地址出现了
|
|
|
|
- 此时配置文件地址出现了
|
|
|
|
|
|
|
|
|
|
|
|
```JAVA
|
|
|
|
```java
|
|
|
|
protected String getSelfInitializationConfig() {
|
|
|
|
protected String getSelfInitializationConfig() {
|
|
|
|
// 寻找配置文件
|
|
|
|
// 寻找配置文件
|
|
|
|
return findConfig(getStandardConfigLocations());
|
|
|
|
return findConfig(getStandardConfigLocations());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
```JAVA
|
|
|
|
```java
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
protected String[] getStandardConfigLocations() {
|
|
|
|
protected String[] getStandardConfigLocations() {
|
|
|
|
return new String[] { "logback-test.groovy", "logback-test.xml", "logback.groovy", "logback.xml" };
|
|
|
|
return new String[] { "logback-test.groovy", "logback-test.xml", "logback.groovy", "logback.xml" };
|
|
|
@ -374,7 +364,7 @@ public static LoggingSystem get(ClassLoader classLoader) {
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
```JAVA
|
|
|
|
```java
|
|
|
|
private String findConfig(String[] locations) {
|
|
|
|
private String findConfig(String[] locations) {
|
|
|
|
for (String location : locations) {
|
|
|
|
for (String location : locations) {
|
|
|
|
ClassPathResource resource = new ClassPathResource(location, this.classLoader);
|
|
|
|
ClassPathResource resource = new ClassPathResource(location, this.classLoader);
|
|
|
@ -384,14 +374,13 @@ public static LoggingSystem get(ClassLoader classLoader) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
- 此时自定义配置文件如何获取的已经明了
|
|
|
|
- 此时自定义配置文件如何获取的已经明了。
|
|
|
|
|
|
|
|
|
|
|
|
#### reinitialize
|
|
|
|
#### reinitialize
|
|
|
|
|
|
|
|
|
|
|
|
```JAVA
|
|
|
|
```java
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
protected void reinitialize(LoggingInitializationContext initializationContext) {
|
|
|
|
protected void reinitialize(LoggingInitializationContext initializationContext) {
|
|
|
|
// 日志上下文重新设置
|
|
|
|
// 日志上下文重新设置
|
|
|
@ -400,10 +389,9 @@ public static LoggingSystem get(ClassLoader classLoader) {
|
|
|
|
// 加载配置文件
|
|
|
|
// 加载配置文件
|
|
|
|
loadConfiguration(initializationContext, getSelfInitializationConfig(), null);
|
|
|
|
loadConfiguration(initializationContext, getSelfInitializationConfig(), null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
```JAVA
|
|
|
|
```java
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
protected void loadConfiguration(LoggingInitializationContext initializationContext, String location,
|
|
|
|
protected void loadConfiguration(LoggingInitializationContext initializationContext, String location,
|
|
|
|
LogFile logFile) {
|
|
|
|
LogFile logFile) {
|
|
|
@ -453,4 +441,4 @@ public static LoggingSystem get(ClassLoader classLoader) {
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
- 执行配置属于 logback 操作源码不在此进行分析
|
|
|
|
执行配置属于 logback 操作源码不在此进行分析。
|
|
|
|