From 815d7de222bdc1032545687279e67b6e5dcfc270 Mon Sep 17 00:00:00 2001 From: Ben Hale Date: Fri, 15 May 2020 12:47:35 -0700 Subject: [PATCH] Deferred Logging Previously the logging put out by the library happened synchronously with the code executing. Because this library typically runs so early in the Spring Boot lifecycle that logging isn't yet configured, it needs to defer that logging until later. This change converts the library to use Boot's DeferredLogger and listen for an ApplicationPrepared event before logging out its messages. Signed-off-by: Ben Hale --- ...dingFlattenedEnvironmentPostProcessor.java | 27 ++++++++++++------- ...ndingSpecificEnvironmentPostProcessor.java | 27 ++++++++++++------- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/src/main/java/org/springframework/cloud/bindings/boot/BindingFlattenedEnvironmentPostProcessor.java b/src/main/java/org/springframework/cloud/bindings/boot/BindingFlattenedEnvironmentPostProcessor.java index 69ff7e8..c1d6267 100644 --- a/src/main/java/org/springframework/cloud/bindings/boot/BindingFlattenedEnvironmentPostProcessor.java +++ b/src/main/java/org/springframework/cloud/bindings/boot/BindingFlattenedEnvironmentPostProcessor.java @@ -16,12 +16,13 @@ package org.springframework.cloud.bindings.boot; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.context.config.ConfigFileApplicationListener; +import org.springframework.boot.context.event.ApplicationPreparedEvent; import org.springframework.boot.env.EnvironmentPostProcessor; +import org.springframework.boot.logging.DeferredLog; import org.springframework.cloud.bindings.Bindings; +import org.springframework.context.ApplicationListener; import org.springframework.core.Ordered; import org.springframework.core.env.ConfigurableEnvironment; @@ -34,11 +35,12 @@ import static org.springframework.cloud.bindings.boot.PropertySourceContributor. * An implementation of {@link EnvironmentPostProcessor} that generates properties from {@link Bindings} with a * flattened format: {@code cnb.bindings.{name}.{metadata,secret}.*}. */ -public final class BindingFlattenedEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered { +public final class BindingFlattenedEnvironmentPostProcessor implements ApplicationListener, + EnvironmentPostProcessor, Ordered { public static final String BINDING_FLATTENED_PROPERTY_SOURCE_NAME = "cnbBindingFlattened"; - private final Log log = LogFactory.getLog(getClass()); + private final DeferredLog log = new DeferredLog(); private final Bindings bindings; @@ -54,6 +56,17 @@ public final class BindingFlattenedEnvironmentPostProcessor implements Environme this.bindings = bindings; } + @Override + public int getOrder() { + // Before ConfigFileApplicationListener so values there can use values from {@link Bindings}. + return ConfigFileApplicationListener.DEFAULT_ORDER - 1; + } + + @Override + public void onApplicationEvent(ApplicationPreparedEvent event) { + this.log.switchTo(getClass()); + } + @Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { Map properties = new HashMap<>(); @@ -75,10 +88,4 @@ public final class BindingFlattenedEnvironmentPostProcessor implements Environme contributePropertySource(BINDING_FLATTENED_PROPERTY_SOURCE_NAME, properties, environment); } - @Override - public int getOrder() { - // Before ConfigFileApplicationListener so values there can use values from {@link Bindings}. - return ConfigFileApplicationListener.DEFAULT_ORDER - 1; - } - } diff --git a/src/main/java/org/springframework/cloud/bindings/boot/BindingSpecificEnvironmentPostProcessor.java b/src/main/java/org/springframework/cloud/bindings/boot/BindingSpecificEnvironmentPostProcessor.java index def1f88..8b62b2a 100644 --- a/src/main/java/org/springframework/cloud/bindings/boot/BindingSpecificEnvironmentPostProcessor.java +++ b/src/main/java/org/springframework/cloud/bindings/boot/BindingSpecificEnvironmentPostProcessor.java @@ -16,12 +16,13 @@ package org.springframework.cloud.bindings.boot; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.context.config.ConfigFileApplicationListener; +import org.springframework.boot.context.event.ApplicationPreparedEvent; import org.springframework.boot.env.EnvironmentPostProcessor; +import org.springframework.boot.logging.DeferredLog; import org.springframework.cloud.bindings.Bindings; +import org.springframework.context.ApplicationListener; import org.springframework.core.Ordered; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.PropertySource; @@ -44,7 +45,8 @@ import static org.springframework.cloud.bindings.boot.PropertySourceContributor. * Must be enabled by setting the {@code org.springframework.cloud.bindings.boot.enable} System Property to * {@code true}. */ -public final class BindingSpecificEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered { +public final class BindingSpecificEnvironmentPostProcessor implements ApplicationListener, + EnvironmentPostProcessor, Ordered { /** * The name of the {@link PropertySource} created by the {@code BindingsEnvironmentPostProcessor}: {@value}. @@ -53,7 +55,7 @@ public final class BindingSpecificEnvironmentPostProcessor implements Environmen final List processors; - private final Log log = LogFactory.getLog(getClass()); + private final DeferredLog log = new DeferredLog(); private final Bindings bindings; @@ -72,6 +74,17 @@ public final class BindingSpecificEnvironmentPostProcessor implements Environmen this.processors = Arrays.asList(processors); } + @Override + public int getOrder() { + // Before ConfigFileApplicationListener so values there can use values from {@link Bindings}. + return ConfigFileApplicationListener.DEFAULT_ORDER - 1; + } + + @Override + public void onApplicationEvent(ApplicationPreparedEvent event) { + this.log.switchTo(getClass()); + } + @Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { if (!isGlobalEnabled(environment)) { @@ -94,10 +107,4 @@ public final class BindingSpecificEnvironmentPostProcessor implements Environmen contributePropertySource(BINDING_SPECIFIC_PROPERTY_SOURCE_NAME, properties, environment); } - @Override - public int getOrder() { - // Before ConfigFileApplicationListener so values there can use values from {@link Bindings}. - return ConfigFileApplicationListener.DEFAULT_ORDER - 1; - } - }