From a551f42234757c7c45eaeeb7f4d7f8404b73ed2e Mon Sep 17 00:00:00 2001 From: Jamin Hitchcock Date: Tue, 22 Jun 2021 20:48:54 -0500 Subject: [PATCH] LoggingSystemShutdownListener ClassLoader Use the ClassLoader from the SpringApplication if available. --- .../LoggingSystemShutdownListener.java | 11 ++++-- .../LoggingSystemShutdownListenerTests.java | 37 +++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/LoggingSystemShutdownListenerTests.java diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/LoggingSystemShutdownListener.java b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/LoggingSystemShutdownListener.java index 7240594e4..059bbbcec 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/LoggingSystemShutdownListener.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/LoggingSystemShutdownListener.java @@ -16,6 +16,9 @@ package org.springframework.cloud.bootstrap; +import java.util.Optional; + +import org.springframework.boot.SpringApplication; import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; import org.springframework.boot.logging.LoggingSystem; import org.springframework.context.ApplicationListener; @@ -42,12 +45,14 @@ public class LoggingSystemShutdownListener @Override public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { - shutdownLogging(); + Optional classLoader = Optional.ofNullable(event) + .map(ApplicationEnvironmentPreparedEvent::getSpringApplication).map(SpringApplication::getClassLoader); + shutdownLogging(classLoader); } - private void shutdownLogging() { + private void shutdownLogging(Optional classLoader) { // TODO: only enable if bootstrap and legacy - LoggingSystem loggingSystem = LoggingSystem.get(ClassUtils.getDefaultClassLoader()); + LoggingSystem loggingSystem = LoggingSystem.get(classLoader.orElse(ClassUtils.getDefaultClassLoader())); loggingSystem.cleanUp(); loggingSystem.beforeInitialize(); } diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/LoggingSystemShutdownListenerTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/LoggingSystemShutdownListenerTests.java new file mode 100644 index 000000000..1ac9903af --- /dev/null +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/LoggingSystemShutdownListenerTests.java @@ -0,0 +1,37 @@ +/* + * Copyright 2012-2020 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.cloud.bootstrap; + +import org.junit.Test; + +import org.springframework.boot.DefaultBootstrapContext; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; + +public class LoggingSystemShutdownListenerTests { + + @Test + public void defaultClasspath() { + LoggingSystemShutdownListener listener = new LoggingSystemShutdownListener(); + DefaultBootstrapContext bootstrapCtx = new DefaultBootstrapContext(); + SpringApplication application = new SpringApplication(); + ApplicationEnvironmentPreparedEvent event = new ApplicationEnvironmentPreparedEvent(bootstrapCtx, application, + null, null); + listener.onApplicationEvent(event); + } + +}