Skip to content

Commit

Permalink
LoggingSystemShutdownListener ClassLoader
Browse files Browse the repository at this point in the history
Use the ClassLoader from the SpringApplication if available.
  • Loading branch information
jaminh committed Oct 22, 2021
1 parent 95ce587 commit a551f42
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -42,12 +45,14 @@ public class LoggingSystemShutdownListener

@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
shutdownLogging();
Optional<ClassLoader> classLoader = Optional.ofNullable(event)
.map(ApplicationEnvironmentPreparedEvent::getSpringApplication).map(SpringApplication::getClassLoader);
shutdownLogging(classLoader);
}

private void shutdownLogging() {
private void shutdownLogging(Optional<ClassLoader> 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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}

0 comments on commit a551f42

Please sign in to comment.