我有一个Spring Batch作业,负责处理传入的客户文件。其中一个要求是,每个作业运行(由客户)完成日志文件的记录。
在我的应用程序的主要部分中,我处理命令行参数,然后从那里动态创建我的FileAppender。
我的logback.xml:
<configuration>
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="Console" />
</root>
</configuration>
我的代码添加了appender:
private static void setupFileAppender() {
String logDir = fetchLogDir();
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
String datePortion = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
FileAppender<ILoggingEvent> fileAppender = new FileAppender<>();
fileAppender.setContext(loggerContext);
fileAppender.setName("File");
fileAppender.setFile(logDir + baseFileName + "-" + datePortion + ".log");
fileAppender.setAppend(true);
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(loggerContext);
encoder.setPattern("%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg%n");
encoder.start();
fileAppender.setEncoder(encoder);
fileAppender.start();
Logger rootLogger = loggerContext.getLogger("root");
rootLogger.addAppender(fileAppender);
log.info("Logging configured.");
}
从我的main(或从它调用)执行的任何日志语句都按预期记录到文件。我可以在调试模式下向下钻取,看到我在根记录器上有两个appender - &#34; Console&#34;和&#34;文件&#34;来自两种配置的appenders。但是,一旦我运行SpringApplication.run
命令,FileAppender
就会消失。
我介绍了SpringApplicaton.run(...)
方法,我发现Spring正在重置我的日志配置并从logback.xml重新加载。
来自SpringApplication:
try {
// Create and configure the environment
ConfigurableEnvironment environment = getOrCreateEnvironment();
configureEnvironment(environment, args);
for (SpringApplicationRunListener runListener : runListeners) {
runListener.environmentPrepared(environment);
}
...
来自EventPublishingRunListener
:
@Override
public void environmentPrepared(ConfigurableEnvironment environment) {
publishEvent(new ApplicationEnvironmentPreparedEvent(this.application, this.args,
environment));
}
private void publishEvent(SpringApplicationEvent event) {
this.multicaster.multicastEvent(event);
}
稍后几个电话,然后是LoggingApplicationListener
:
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ApplicationStartedEvent) {
onApplicationStartedEvent((ApplicationStartedEvent) event);
}
else if (event instanceof ApplicationEnvironmentPreparedEvent) {
onApplicationPreparedEvent((ApplicationEnvironmentPreparedEvent) event);
}
}
private void onApplicationPreparedEvent(ApplicationEnvironmentPreparedEvent event) {
if (this.loggingSystem == null) {
this.loggingSystem = LoggingSystem.get(event.getSpringApplication()
.getClassLoader());
}
initialize(event.getEnvironment(), event.getSpringApplication().getClassLoader());
}
protected void initialize(ConfigurableEnvironment environment, ClassLoader classLoader) {
if (System.getProperty(PID_KEY) == null) {
System.setProperty(PID_KEY, new ApplicationPid().toString());
}
initializeEarlyLoggingLevel(environment);
initializeSystem(environment, this.loggingSystem);
initializeFinalLoggingLevels(environment, this.loggingSystem);
}
private void initializeSystem(ConfigurableEnvironment environment,
LoggingSystem system) {
LogFile logFile = LogFile.get(environment);
String logConfig = environment.getProperty(CONFIG_PROPERTY);
if (StringUtils.hasLength(logConfig)) {
try {
ResourceUtils.getURL(logConfig).openStream().close();
system.initialize(logConfig, logFile);
}
catch (Exception ex) {
this.logger.warn("Logging environment value '" + logConfig
+ "' cannot be opened and will be ignored "
+ "(using default location instead)");
system.initialize(null, logFile);
}
}
else {
system.initialize(null, logFile);
}
}
在LogbackLoggingSystem
(和AbstractLoggingSystem
)中:
@Override
public void initialize(String configLocation, LogFile logFile) {
getLogger(null).getLoggerContext().getTurboFilterList().remove(FILTER);
super.initialize(configLocation, logFile);
}
@Override
public void initialize(String configLocation, LogFile logFile) {
if (StringUtils.hasLength(configLocation)) {
// Load a specific configuration
configLocation = SystemPropertyUtils.resolvePlaceholders(configLocation);
loadConfiguration(configLocation, logFile);
}
else {
String selfInitializationConfig = getSelfInitializationConfig();
if (selfInitializationConfig == null) {
// No self initialization has occurred, use defaults
loadDefaults(logFile);
}
else if (logFile != null) {
// Self initialization has occurred but the file has changed, reload
loadConfiguration(selfInitializationConfig, logFile);
}
else {
reinitialize();
}
}
}
最后一个被击中,并且reinitialize()
被称为:
@Override
protected void reinitialize() {
getLoggerContext().reset();
loadConfiguration(getSelfInitializationConfig(), null);
}
对上下文重置的调用是重置所有内容的。问题是,挖掘loadConfiguration
方法也会将reset方法称为日志记录上下文。
有关如何解决Spring重置日志配置的任何想法?
仅供参考,我使用的是4.1.4.RELEASE版本的Spring。
答案 0 :(得分:4)
这听起来像是在LoggingApplicationListener
运行之后推迟自定义日志记录配置。
LoggingApplicationListener
执行初始化以响应ApplicationEnvironmentPreparedEvent
并且订单为Ordered.HIGHEST_PRECEDENCE + 11
。为防止覆盖自定义配置,您可以将自定义逻辑封装在SmartApplicationListener
中,该LoggingApplicationListener
响应同一事件,但命令较低,以便在public class CustomLoggingConfigurationApplicationListener implements
SmartApplicationListener {
@Override
public void onApplicationEvent(ApplicationEvent event) {
// Customise the logging configuration
}
@Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE + 12;
}
@Override
public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
return ApplicationEnvironmentPreparedEvent.class.isAssignableFrom(eventType);
}
@Override
public boolean supportsSourceType(Class<?> sourceType) {
return true;
}
}
之后运行:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class)
.listeners(new CustomLoggingConfigurationApplicationListener())
.run(args);
}
}
您可以创建监听器并在应用程序的主要方法中注册它:
{{1}}