🐛 Fixing ApplicationLoggerInitializer use EnvironmentPostProcessor.

This commit is contained in:
如梦技术 2021-05-18 17:10:54 +08:00
parent cd449d2f37
commit 84f5e6ee37
2 changed files with 12 additions and 8 deletions

View File

@ -16,8 +16,9 @@
package com.pig4cloud.pig.common.log.init;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
/**
@ -26,17 +27,20 @@ import org.springframework.core.env.ConfigurableEnvironment;
* <p>
* 通过环境变量的形式注入 logging.file 自动维护 Spring Boot Admin Logger Viewer
*/
public class ApplicationLoggerInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
public class ApplicationLoggerInitializer implements EnvironmentPostProcessor, Ordered {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
ConfigurableEnvironment environment = applicationContext.getEnvironment();
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
String appName = environment.getProperty("spring.application.name");
String logBase = environment.getProperty("LOGGING_PATH", "logs");
// spring boot admin 直接加载日志
System.setProperty("logging.file.name", String.format("%s/%s/debug.log", logBase, appName));
}
@Override
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE;
}
}

View File

@ -1,4 +1,4 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.pig4cloud.pig.common.log.LogAutoConfiguration
org.springframework.context.ApplicationContextInitializer=\
org.springframework.boot.env.EnvironmentPostProcessor=\
com.pig4cloud.pig.common.log.init.ApplicationLoggerInitializer