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 <bhale@vmware.com>
This commit is contained in:
Ben Hale
2020-05-15 12:47:35 -07:00
parent 03f17f2bd6
commit 815d7de222
2 changed files with 34 additions and 20 deletions

View File

@@ -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<ApplicationPreparedEvent>,
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<String, Object> 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;
}
}

View File

@@ -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<ApplicationPreparedEvent>,
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<BindingsPropertiesProcessor> 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;
}
}