Skip to content

Commit 5cf2e76

Browse files
committed
Set PID system property before *_LOG_PATTERN system properties
Previously, the PID system property was set after the CONSOLE_LOG_PATTERN and FILE_LOG_PATTERN system properties. This meant that the values of the pattern system properties could not reference the PID system property, i.e. ${PID} would not resolve. This commit sets the PID system property before the *_LOG_PATTERN system properties, thereby allowing the latter to reference the former. Closes gh-10594
1 parent 137d3f3 commit 5cf2e76

File tree

2 files changed

+99
-1
lines changed

2 files changed

+99
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ public void apply(LogFile logFile) {
5353
.ignoringUnresolvableNestedPlaceholders(this.environment, "logging.");
5454
setSystemProperty(propertyResolver, EXCEPTION_CONVERSION_WORD,
5555
"exception-conversion-word");
56+
setSystemProperty(PID_KEY, new ApplicationPid().toString());
5657
setSystemProperty(propertyResolver, CONSOLE_LOG_PATTERN, "pattern.console");
5758
setSystemProperty(propertyResolver, FILE_LOG_PATTERN, "pattern.file");
5859
setSystemProperty(propertyResolver, LOG_LEVEL_PATTERN, "pattern.level");
59-
setSystemProperty(PID_KEY, new ApplicationPid().toString());
6060
if (logFile != null) {
6161
logFile.applyToSystemProperties();
6262
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.logging;
18+
19+
import java.util.Collections;
20+
import java.util.HashSet;
21+
import java.util.Set;
22+
23+
import org.junit.After;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
27+
import org.springframework.core.env.Environment;
28+
import org.springframework.core.env.MapPropertySource;
29+
import org.springframework.core.env.StandardEnvironment;
30+
import org.springframework.mock.env.MockEnvironment;
31+
32+
import static org.assertj.core.api.Assertions.assertThat;
33+
34+
/**
35+
* Tests for {@link LoggingSystemProperties}.
36+
*
37+
* @author Andy Wilkinson
38+
*/
39+
public class LoggingSystemPropertiesTests {
40+
41+
private Set<Object> systemPropertyNames;
42+
43+
@Before
44+
public void captureSystemPropertyNames() {
45+
this.systemPropertyNames = new HashSet<Object>(System.getProperties().keySet());
46+
}
47+
48+
@After
49+
public void restoreSystemProperties() {
50+
System.getProperties().keySet().retainAll(this.systemPropertyNames);
51+
}
52+
53+
@Test
54+
public void pidIsSet() {
55+
new LoggingSystemProperties(new MockEnvironment()).apply(null);
56+
assertThat(System.getProperty(LoggingSystemProperties.PID_KEY)).isNotNull();
57+
}
58+
59+
@Test
60+
public void consoleLogPatternIsSet() {
61+
new LoggingSystemProperties(new MockEnvironment()
62+
.withProperty("logging.pattern.console", "console pattern")).apply(null);
63+
assertThat(System.getProperty(LoggingSystemProperties.CONSOLE_LOG_PATTERN))
64+
.isEqualTo("console pattern");
65+
}
66+
67+
@Test
68+
public void fileLogPatternIsSet() {
69+
new LoggingSystemProperties(new MockEnvironment()
70+
.withProperty("logging.pattern.file", "file pattern")).apply(null);
71+
assertThat(System.getProperty(LoggingSystemProperties.FILE_LOG_PATTERN))
72+
.isEqualTo("file pattern");
73+
}
74+
75+
@Test
76+
public void consoleLogPatternCanReferencePid() {
77+
new LoggingSystemProperties(
78+
environment("logging.pattern.console", "${PID:unknown}")).apply(null);
79+
assertThat(System.getProperty(LoggingSystemProperties.CONSOLE_LOG_PATTERN))
80+
.matches("[0-9]+");
81+
}
82+
83+
@Test
84+
public void fileLogPatternCanReferencePid() {
85+
new LoggingSystemProperties(environment("logging.pattern.file", "${PID:unknown}"))
86+
.apply(null);
87+
assertThat(System.getProperty(LoggingSystemProperties.FILE_LOG_PATTERN))
88+
.matches("[0-9]+");
89+
}
90+
91+
private Environment environment(String key, Object value) {
92+
StandardEnvironment environment = new StandardEnvironment();
93+
environment.getPropertySources().addLast(
94+
new MapPropertySource("test", Collections.singletonMap(key, value)));
95+
return environment;
96+
}
97+
98+
}

0 commit comments

Comments
 (0)