This commit is contained in:
Phillip Webb
2020-08-06 11:18:53 -07:00
parent 1692899cd0
commit dfd233cada
3 changed files with 33 additions and 49 deletions

View File

@@ -19,6 +19,7 @@ package org.springframework.boot;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;
import org.apache.commons.logging.Log;
@@ -49,59 +50,36 @@ class SpringApplicationRunListeners {
}
void starting() {
StartupStep starting = this.applicationStartup.start("spring.boot.application.starting");
for (SpringApplicationRunListener listener : this.listeners) {
listener.starting();
}
starting.end();
doWithListeners("spring.boot.application.starting", SpringApplicationRunListener::starting);
}
void environmentPrepared(ConfigurableEnvironment environment) {
StartupStep environmentPrepared = this.applicationStartup.start("spring.boot.application.environment-prepared");
for (SpringApplicationRunListener listener : this.listeners) {
listener.environmentPrepared(environment);
}
environmentPrepared.end();
doWithListeners("spring.boot.application.environment-prepared",
(listener) -> listener.environmentPrepared(environment));
}
void contextPrepared(ConfigurableApplicationContext context) {
StartupStep contextPrepared = this.applicationStartup.start("spring.boot.application.context-prepared");
for (SpringApplicationRunListener listener : this.listeners) {
listener.contextPrepared(context);
}
contextPrepared.end();
doWithListeners("spring.boot.application.context-prepared", (listener) -> listener.contextPrepared(context));
}
void contextLoaded(ConfigurableApplicationContext context) {
StartupStep contextLoaded = this.applicationStartup.start("spring.boot.application.context-loaded");
for (SpringApplicationRunListener listener : this.listeners) {
listener.contextLoaded(context);
}
contextLoaded.end();
doWithListeners("spring.boot.application.context-loaded", (listener) -> listener.contextLoaded(context));
}
void started(ConfigurableApplicationContext context) {
StartupStep started = this.applicationStartup.start("spring.boot.application.started");
for (SpringApplicationRunListener listener : this.listeners) {
listener.started(context);
}
started.end();
doWithListeners("spring.boot.application.started", (listener) -> listener.started(context));
}
void running(ConfigurableApplicationContext context) {
StartupStep running = this.applicationStartup.start("spring.boot.application.running");
for (SpringApplicationRunListener listener : this.listeners) {
listener.running(context);
}
running.end();
doWithListeners("spring.boot.application.running", (listener) -> listener.running(context));
}
void failed(ConfigurableApplicationContext context, Throwable exception) {
StartupStep failed = this.applicationStartup.start("spring.boot.application.failed");
for (SpringApplicationRunListener listener : this.listeners) {
callFailedListener(listener, context, exception);
}
failed.tag("exception", exception.getClass().toString()).tag("message", exception.getMessage()).end();
doWithListeners("spring.boot.application.failed",
(listener) -> callFailedListener(listener, context, exception), (step) -> {
step.tag("exception", exception.getClass().toString());
step.tag("message", exception.getMessage());
});
}
private void callFailedListener(SpringApplicationRunListener listener, ConfigurableApplicationContext context,
@@ -124,4 +102,16 @@ class SpringApplicationRunListeners {
}
}
private void doWithListeners(String stepName, Consumer<SpringApplicationRunListener> listenerAction) {
doWithListeners(stepName, listenerAction, StartupStep::end);
}
private void doWithListeners(String stepName, Consumer<SpringApplicationRunListener> listenerAction,
Consumer<StartupStep> stepAction) {
StartupStep step = this.applicationStartup.start(stepName);
this.listeners.forEach(listenerAction);
stepAction.accept(step);
step.end();
}
}

View File

@@ -58,24 +58,20 @@ public abstract class LoggingSystem {
*/
public static final String ROOT_LOGGER_NAME = "ROOT";
private static final Function<ClassLoader, LoggingSystem> SYSTEM_FACTORY;
private static final Function<ClassLoader, LoggingSystem> SYSTEM_FACTORY = getSystemFactory();
static {
private static Function<ClassLoader, LoggingSystem> getSystemFactory() {
ClassLoader classLoader = LoggingSystem.class.getClassLoader();
if (ClassUtils.isPresent("ch.qos.logback.core.Appender", classLoader)) {
SYSTEM_FACTORY = (cl) -> new LogbackLoggingSystem(cl);
return LogbackLoggingSystem::new;
}
else if (ClassUtils.isPresent("org.apache.logging.log4j.core.impl.Log4jContextFactory", classLoader)) {
SYSTEM_FACTORY = (cl) -> new Log4J2LoggingSystem(cl);
if (ClassUtils.isPresent("org.apache.logging.log4j.core.impl.Log4jContextFactory", classLoader)) {
return Log4J2LoggingSystem::new;
}
else if (ClassUtils.isPresent("java.util.logging.LogManager", classLoader)) {
SYSTEM_FACTORY = (cl) -> new JavaLoggingSystem(cl);
}
else {
SYSTEM_FACTORY = (cl) -> {
throw new IllegalStateException("No suitable logging system located");
};
if (ClassUtils.isPresent("java.util.logging.LogManager", classLoader)) {
return JavaLoggingSystem::new;
}
throw new IllegalStateException("No suitable logging system located");
}
/**

View File

@@ -1160,12 +1160,10 @@ class SpringApplicationTests {
given(applicationStartup.start(anyString())).willReturn(startupStep);
given(startupStep.tag(anyString(), anyString())).willReturn(startupStep);
given(startupStep.tag(anyString(), ArgumentMatchers.<Supplier<String>>any())).willReturn(startupStep);
SpringApplication application = new SpringApplication(ExampleConfig.class);
application.setWebApplicationType(WebApplicationType.NONE);
application.setApplicationStartup(applicationStartup);
this.context = application.run();
assertThat(this.context.getBean(ApplicationStartup.class)).isEqualTo(applicationStartup);
verify(applicationStartup).start("spring.boot.application.starting");
verify(applicationStartup).start("spring.boot.application.environment-prepared");