Skip to content

Commit

Permalink
Merge branch '2.6.x' into 2.7.x
Browse files Browse the repository at this point in the history
Closes gh-31963
  • Loading branch information
philwebb committed Aug 3, 2022
2 parents 0dde427 + 45ad155 commit c652d3d
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 54 deletions.
Expand Up @@ -45,6 +45,8 @@
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.SmartLifecycle;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.GenericApplicationListener;
import org.springframework.core.Ordered;
Expand Down Expand Up @@ -136,6 +138,11 @@ public class LoggingApplicationListener implements GenericApplicationListener {
*/
public static final String LOGGER_GROUPS_BEAN_NAME = "springBootLoggerGroups";

/**
* The name of the {@link Lifecycle} bean used to handle cleanup.
*/
private static final String LOGGING_LIFECYCLE_BEAN_NAME = "springBootLoggingLifecycle";

private static final Map<String, List<String>> DEFAULT_GROUP_LOGGERS;
static {
MultiValueMap<String, String> loggers = new LinkedMultiValueMap<>();
Expand Down Expand Up @@ -218,9 +225,8 @@ else if (event instanceof ApplicationEnvironmentPreparedEvent) {
else if (event instanceof ApplicationPreparedEvent) {
onApplicationPreparedEvent((ApplicationPreparedEvent) event);
}
else if (event instanceof ContextClosedEvent
&& ((ContextClosedEvent) event).getApplicationContext().getParent() == null) {
onContextClosedEvent();
else if (event instanceof ContextClosedEvent) {
onContextClosedEvent((ContextClosedEvent) event);
}
else if (event instanceof ApplicationFailedEvent) {
onApplicationFailedEvent();
Expand All @@ -241,7 +247,8 @@ private void onApplicationEnvironmentPreparedEvent(ApplicationEnvironmentPrepare
}

private void onApplicationPreparedEvent(ApplicationPreparedEvent event) {
ConfigurableListableBeanFactory beanFactory = event.getApplicationContext().getBeanFactory();
ConfigurableApplicationContext applicationContext = event.getApplicationContext();
ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory();
if (!beanFactory.containsBean(LOGGING_SYSTEM_BEAN_NAME)) {
beanFactory.registerSingleton(LOGGING_SYSTEM_BEAN_NAME, this.loggingSystem);
}
Expand All @@ -251,20 +258,29 @@ private void onApplicationPreparedEvent(ApplicationPreparedEvent event) {
if (this.loggerGroups != null && !beanFactory.containsBean(LOGGER_GROUPS_BEAN_NAME)) {
beanFactory.registerSingleton(LOGGER_GROUPS_BEAN_NAME, this.loggerGroups);
}
if (!beanFactory.containsBean(LOGGING_LIFECYCLE_BEAN_NAME) && applicationContext.getParent() == null) {
beanFactory.registerSingleton(LOGGING_LIFECYCLE_BEAN_NAME, new Lifecycle());
}
}

private void onContextClosedEvent() {
if (this.loggingSystem != null) {
this.loggingSystem.cleanUp();
private void onContextClosedEvent(ContextClosedEvent event) {
ApplicationContext applicationContext = event.getApplicationContext();
if (applicationContext.getParent() != null || applicationContext.containsBean(LOGGING_LIFECYCLE_BEAN_NAME)) {
return;
}
cleanupLoggingSystem();
}

private void onApplicationFailedEvent() {
void cleanupLoggingSystem() {
if (this.loggingSystem != null) {
this.loggingSystem.cleanUp();
}
}

private void onApplicationFailedEvent() {
cleanupLoggingSystem();
}

/**
* Initialize the logging system according to preferences expressed through the
* {@link Environment} and the classpath.
Expand Down Expand Up @@ -438,4 +454,32 @@ public void setParseArgs(boolean parseArgs) {
this.parseArgs = parseArgs;
}

private class Lifecycle implements SmartLifecycle {

private volatile boolean running;

@Override
public void start() {
this.running = true;
}

@Override
public void stop() {
this.running = false;
cleanupLoggingSystem();
}

@Override
public boolean isRunning() {
return this.running;
}

@Override
public int getPhase() {
// Shutdown late and always after WebServerStartStopLifecycle
return Integer.MIN_VALUE + 1;
}

}

}

0 comments on commit c652d3d

Please sign in to comment.