Skip to content

Commit a551f42

Browse files
committed
LoggingSystemShutdownListener ClassLoader
Use the ClassLoader from the SpringApplication if available.
1 parent 95ce587 commit a551f42

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/LoggingSystemShutdownListener.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package org.springframework.cloud.bootstrap;
1818

19+
import java.util.Optional;
20+
21+
import org.springframework.boot.SpringApplication;
1922
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
2023
import org.springframework.boot.logging.LoggingSystem;
2124
import org.springframework.context.ApplicationListener;
@@ -42,12 +45,14 @@ public class LoggingSystemShutdownListener
4245

4346
@Override
4447
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
45-
shutdownLogging();
48+
Optional<ClassLoader> classLoader = Optional.ofNullable(event)
49+
.map(ApplicationEnvironmentPreparedEvent::getSpringApplication).map(SpringApplication::getClassLoader);
50+
shutdownLogging(classLoader);
4651
}
4752

48-
private void shutdownLogging() {
53+
private void shutdownLogging(Optional<ClassLoader> classLoader) {
4954
// TODO: only enable if bootstrap and legacy
50-
LoggingSystem loggingSystem = LoggingSystem.get(ClassUtils.getDefaultClassLoader());
55+
LoggingSystem loggingSystem = LoggingSystem.get(classLoader.orElse(ClassUtils.getDefaultClassLoader()));
5156
loggingSystem.cleanUp();
5257
loggingSystem.beforeInitialize();
5358
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.bootstrap;
18+
19+
import org.junit.Test;
20+
21+
import org.springframework.boot.DefaultBootstrapContext;
22+
import org.springframework.boot.SpringApplication;
23+
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
24+
25+
public class LoggingSystemShutdownListenerTests {
26+
27+
@Test
28+
public void defaultClasspath() {
29+
LoggingSystemShutdownListener listener = new LoggingSystemShutdownListener();
30+
DefaultBootstrapContext bootstrapCtx = new DefaultBootstrapContext();
31+
SpringApplication application = new SpringApplication();
32+
ApplicationEnvironmentPreparedEvent event = new ApplicationEnvironmentPreparedEvent(bootstrapCtx, application,
33+
null, null);
34+
listener.onApplicationEvent(event);
35+
}
36+
37+
}

0 commit comments

Comments
 (0)