Skip to content

Commit ed424d3

Browse files
committed
Polish 'Add Log4J2 PropertySource backed by the Spring Environment'
See gh-32733
1 parent 4f8a944 commit ed424d3

File tree

4 files changed

+122
-19
lines changed

4 files changed

+122
-19
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,10 @@ public void initialize(LoggingInitializationContext initializationContext, Strin
233233
return;
234234
}
235235
Environment environment = initializationContext.getEnvironment();
236-
getLoggerContext().putObjectIfAbsent(ENVIRONMENT_KEY, environment);
237-
PropertiesUtil.getProperties().addPropertySource(new SpringPropertySource(environment));
236+
if (environment != null) {
237+
getLoggerContext().putObjectIfAbsent(ENVIRONMENT_KEY, environment);
238+
PropertiesUtil.getProperties().addPropertySource(new SpringEnvironmentPropertySource(environment));
239+
}
238240
loggerContext.getConfiguration().removeFilter(FILTER);
239241
super.initialize(initializationContext, configLocation, logFile);
240242
markAsInitialized(loggerContext);
+11-17
Original file line numberDiff line numberDiff line change
@@ -19,46 +19,40 @@
1919
import org.apache.logging.log4j.util.PropertySource;
2020

2121
import org.springframework.core.env.Environment;
22+
import org.springframework.util.Assert;
2223

2324
/**
2425
* Returns properties from Spring.
2526
*
2627
* @author Ralph Goers
27-
* @since 3.0.0
2828
*/
29-
public class SpringPropertySource implements PropertySource {
29+
class SpringEnvironmentPropertySource implements PropertySource {
3030

31-
private static final int DEFAULT_PRIORITY = -100;
31+
/**
32+
* System properties take precedence followed by properties in Log4j properties files.
33+
*/
34+
private static final int PRIORITY = -100;
3235

3336
private final Environment environment;
3437

35-
public SpringPropertySource(Environment environment) {
38+
SpringEnvironmentPropertySource(Environment environment) {
39+
Assert.notNull(environment, "Environment must not be null");
3640
this.environment = environment;
3741
}
3842

39-
/**
40-
* System properties take precedence followed by properties in Log4j properties files.
41-
* @return this PropertySource's priority.
42-
*/
4343
@Override
4444
public int getPriority() {
45-
return DEFAULT_PRIORITY;
45+
return PRIORITY;
4646
}
4747

4848
@Override
4949
public String getProperty(String key) {
50-
if (this.environment != null) {
51-
return this.environment.getProperty(key);
52-
}
53-
return null;
50+
return this.environment.getProperty(key);
5451
}
5552

5653
@Override
5754
public boolean containsProperty(String key) {
58-
if (this.environment != null) {
59-
return this.environment.containsProperty(key);
60-
}
61-
return false;
55+
return this.environment.containsProperty(key);
6256
}
6357

6458
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java

+24
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.LinkedHashMap;
2626
import java.util.List;
2727
import java.util.Map;
28+
import java.util.Set;
2829
import java.util.logging.Handler;
2930
import java.util.logging.Level;
3031

@@ -42,6 +43,7 @@
4243
import org.apache.logging.log4j.core.util.ShutdownCallbackRegistry;
4344
import org.apache.logging.log4j.jul.Log4jBridgeHandler;
4445
import org.apache.logging.log4j.util.PropertiesUtil;
46+
import org.apache.logging.log4j.util.PropertySource;
4547
import org.junit.jupiter.api.AfterEach;
4648
import org.junit.jupiter.api.BeforeEach;
4749
import org.junit.jupiter.api.Test;
@@ -59,6 +61,7 @@
5961
import org.springframework.boot.testsupport.system.OutputCaptureExtension;
6062
import org.springframework.core.env.Environment;
6163
import org.springframework.mock.env.MockEnvironment;
64+
import org.springframework.test.util.ReflectionTestUtils;
6265
import org.springframework.util.ClassUtils;
6366
import org.springframework.util.StringUtils;
6467

@@ -101,6 +104,7 @@ void setup() {
101104
this.configuration = loggerContext.getConfiguration();
102105
this.loggingSystem.cleanUp();
103106
this.logger = LogManager.getLogger(getClass());
107+
cleanUpPropertySources();
104108
}
105109

106110
@AfterEach
@@ -109,6 +113,16 @@ void cleanUp() {
109113
LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
110114
loggerContext.stop();
111115
loggerContext.start(((Reconfigurable) this.configuration).reconfigure());
116+
cleanUpPropertySources();
117+
}
118+
119+
@SuppressWarnings("unchecked")
120+
private void cleanUpPropertySources() { // https://issues.apache.org/jira/browse/LOG4J2-3618
121+
PropertiesUtil properties = PropertiesUtil.getProperties();
122+
Object environment = ReflectionTestUtils.getField(properties, "environment");
123+
Set<PropertySource> sources = (Set<PropertySource>) ReflectionTestUtils.getField(environment, "sources");
124+
sources.removeIf((candidate) -> candidate instanceof SpringEnvironmentPropertySource
125+
|| candidate instanceof SpringBootPropertySource);
112126
}
113127

114128
@Test
@@ -448,6 +462,16 @@ void initializeAttachesEnvironmentToLoggerContext() {
448462
assertThat(environment).isSameAs(this.environment);
449463
}
450464

465+
@Test
466+
void initializeAddsSpringEnvironmentPropertySource() {
467+
PropertiesUtil properties = PropertiesUtil.getProperties();
468+
this.environment.setProperty("spring", "boot");
469+
this.loggingSystem.beforeInitialize();
470+
this.loggingSystem.initialize(this.initializationContext, null, null);
471+
properties = PropertiesUtil.getProperties();
472+
assertThat(properties.getStringProperty("spring")).isEqualTo("boot");
473+
}
474+
451475
private String getRelativeClasspathLocation(String fileName) {
452476
String defaultPath = ClassUtils.getPackageName(getClass());
453477
defaultPath = defaultPath.replace('.', '/');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2012-2022 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+
* https://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.log4j2;
18+
19+
import java.util.Properties;
20+
21+
import org.apache.logging.log4j.util.PropertiesPropertySource;
22+
import org.apache.logging.log4j.util.SystemPropertiesPropertySource;
23+
import org.junit.jupiter.api.BeforeEach;
24+
import org.junit.jupiter.api.Test;
25+
26+
import org.springframework.mock.env.MockEnvironment;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
30+
31+
/**
32+
* Tests for {@link SpringEnvironmentPropertySource}.
33+
*
34+
* @author Phillip Webb
35+
*/
36+
class SpringEnvironmentPropertySourceTests {
37+
38+
private MockEnvironment environment;
39+
40+
private SpringEnvironmentPropertySource propertySource;
41+
42+
@BeforeEach
43+
void setup() {
44+
this.environment = new MockEnvironment();
45+
this.environment.setProperty("spring", "boot");
46+
this.propertySource = new SpringEnvironmentPropertySource(this.environment);
47+
}
48+
49+
@Test
50+
void createWhenEnvironmentIsNullThrowsException() {
51+
assertThatIllegalArgumentException().isThrownBy(() -> new SpringEnvironmentPropertySource(null))
52+
.withMessage("Environment must not be null");
53+
}
54+
55+
@Test
56+
void getPriorityIsOrderedCorrectly() {
57+
int priority = this.propertySource.getPriority();
58+
assertThat(priority).isEqualTo(-100);
59+
assertThat(priority).isLessThan(new SystemPropertiesPropertySource().getPriority());
60+
assertThat(priority).isLessThan(new PropertiesPropertySource(new Properties()).getPriority());
61+
}
62+
63+
@Test
64+
void getPropertyWhenInEnvironmentReturnsValue() {
65+
assertThat(this.propertySource.getProperty("spring")).isEqualTo("boot");
66+
}
67+
68+
@Test
69+
void getPropertyWhenNotInEnvironmentReturnsNull() {
70+
assertThat(this.propertySource.getProperty("nope")).isNull();
71+
}
72+
73+
@Test
74+
void containsPropertyWhenInEnvironmentReturnsTrue() {
75+
assertThat(this.propertySource.containsProperty("spring")).isTrue();
76+
}
77+
78+
@Test
79+
void containsPropertyWhenNotInEnvironmentReturnsFalse() {
80+
assertThat(this.propertySource.containsProperty("nope")).isFalse();
81+
}
82+
83+
}

0 commit comments

Comments
 (0)