Skip to content

Commit c23d8fb

Browse files
committed
Ignore unresolvable placeholder in log properties
Update the RelaxedPropertyResolver used to load log properties so that `${...}` patterns are ignored when possible. Fixes gh-7719
1 parent a2cf7be commit c23d8fb

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

spring-boot/src/main/java/org/springframework/boot/bind/RelaxedPropertyResolver.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import java.util.Map;
2020

2121
import org.springframework.core.env.ConfigurableEnvironment;
22+
import org.springframework.core.env.Environment;
2223
import org.springframework.core.env.PropertyResolver;
24+
import org.springframework.core.env.PropertySourcesPropertyResolver;
2325
import org.springframework.util.Assert;
2426

2527
/**
@@ -145,4 +147,25 @@ public Map<String, Object> getSubProperties(String keyPrefix) {
145147
keyPrefix);
146148
}
147149

150+
/**
151+
* Return a property resolver for the environment, preferring one that ignores
152+
* unresolvable nested placeholders.
153+
* @param environment the source environment
154+
* @param prefix the prefix
155+
* @return a property resolver for the environment
156+
* @since 1.4.3
157+
*/
158+
public static RelaxedPropertyResolver ignoringUnresolvableNestedPlaceholders(
159+
Environment environment, String prefix) {
160+
Assert.notNull(environment, "Environment must not be null");
161+
PropertyResolver resolver = environment;
162+
if (environment instanceof ConfigurableEnvironment) {
163+
resolver = new PropertySourcesPropertyResolver(
164+
((ConfigurableEnvironment) environment).getPropertySources());
165+
((PropertySourcesPropertyResolver) resolver)
166+
.setIgnoreUnresolvableNestedPlaceholders(true);
167+
}
168+
return new RelaxedPropertyResolver(resolver, prefix);
169+
}
170+
148171
}

spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperties.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ public void apply() {
4949
}
5050

5151
public void apply(LogFile logFile) {
52-
RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(
53-
this.environment, "logging.");
52+
RelaxedPropertyResolver propertyResolver = RelaxedPropertyResolver
53+
.ignoringUnresolvableNestedPlaceholders(this.environment, "logging.");
5454
setSystemProperty(propertyResolver, EXCEPTION_CONVERSION_WORD,
5555
"exception-conversion-word");
5656
setSystemProperty(propertyResolver, CONSOLE_LOG_PATTERN, "pattern.console");

spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ private PropertyResolver getPatternsResolver(Environment environment) {
7272
if (environment == null) {
7373
return new PropertySourcesPropertyResolver(null);
7474
}
75-
return new RelaxedPropertyResolver(environment, "logging.pattern.");
75+
return RelaxedPropertyResolver.ignoringUnresolvableNestedPlaceholders(environment,
76+
"logging.pattern.");
7677
}
7778

7879
public void apply(LogbackConfigurator config) {

spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,16 @@ public void systemPropertiesAreSetForLoggingConfiguration() {
467467
assertThat(System.getProperty("PID")).isNotNull();
468468
}
469469

470+
@Test
471+
public void environmentPropertiesIgnoreUnresolvablePlaceholders() {
472+
// gh-7719
473+
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
474+
"logging.pattern.console=console ${pid}");
475+
this.initializer.initialize(this.context.getEnvironment(),
476+
this.context.getClassLoader());
477+
assertThat(System.getProperty("CONSOLE_LOG_PATTERN")).isEqualTo("console ${pid}");
478+
}
479+
470480
@Test
471481
public void logFilePropertiesCanReferenceSystemProperties() {
472482
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,

0 commit comments

Comments
 (0)