Skip to content

Commit 1c5ba19

Browse files
committed
Merge branch '3.1.x' into 3.2.x
Closes gh-40326
2 parents 1e0cb25 + a946f66 commit 1c5ba19

File tree

4 files changed

+45
-20
lines changed

4 files changed

+45
-20
lines changed

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ public class Log4J2LoggingSystem extends AbstractLoggingSystem {
8888

8989
private static final String LOG4J_LOG_MANAGER = "org.apache.logging.log4j.jul.LogManager";
9090

91+
private static final SpringEnvironmentPropertySource propertySource = new SpringEnvironmentPropertySource();
92+
9193
static final String ENVIRONMENT_KEY = Conventions.getQualifiedAttributeName(Log4J2LoggingSystem.class,
9294
"environment");
9395

@@ -214,8 +216,9 @@ public void initialize(LoggingInitializationContext initializationContext, Strin
214216
}
215217
Environment environment = initializationContext.getEnvironment();
216218
if (environment != null) {
217-
getLoggerContext().putObjectIfAbsent(ENVIRONMENT_KEY, environment);
218-
PropertiesUtil.getProperties().addPropertySource(new SpringEnvironmentPropertySource(environment));
219+
getLoggerContext().putObject(ENVIRONMENT_KEY, environment);
220+
Log4J2LoggingSystem.propertySource.setEnvironment(environment);
221+
PropertiesUtil.getProperties().addPropertySource(Log4J2LoggingSystem.propertySource);
219222
}
220223
loggerContext.getConfiguration().removeFilter(FILTER);
221224
super.initialize(initializationContext, configLocation, logFile);
@@ -437,6 +440,8 @@ public void cleanUp() {
437440
LoggerContext loggerContext = getLoggerContext();
438441
markAsUninitialized(loggerContext);
439442
loggerContext.getConfiguration().removeFilter(FILTER);
443+
Log4J2LoggingSystem.propertySource.setEnvironment(null);
444+
getLoggerContext().removeObject(ENVIRONMENT_KEY);
440445
}
441446

442447
private LoggerConfig getLogger(String name) {

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.apache.logging.log4j.util.PropertySource;
2020

2121
import org.springframework.core.env.Environment;
22-
import org.springframework.util.Assert;
2322

2423
/**
2524
* Returns properties from Spring.
@@ -33,12 +32,7 @@ class SpringEnvironmentPropertySource implements PropertySource {
3332
*/
3433
private static final int PRIORITY = -100;
3534

36-
private final Environment environment;
37-
38-
SpringEnvironmentPropertySource(Environment environment) {
39-
Assert.notNull(environment, "Environment must not be null");
40-
this.environment = environment;
41-
}
35+
private volatile Environment environment;
4236

4337
@Override
4438
public int getPriority() {
@@ -47,12 +41,18 @@ public int getPriority() {
4741

4842
@Override
4943
public String getProperty(String key) {
50-
return this.environment.getProperty(key);
44+
Environment environment = this.environment;
45+
return (environment != null) ? environment.getProperty(key) : null;
5146
}
5247

5348
@Override
5449
public boolean containsProperty(String key) {
55-
return this.environment.containsProperty(key);
50+
Environment environment = this.environment;
51+
return (environment != null) ? environment.containsProperty(key) : false;
52+
}
53+
54+
void setEnvironment(Environment environment) {
55+
this.environment = environment;
5656
}
5757

5858
}

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

+14
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,20 @@ void initializeAddsSpringEnvironmentPropertySource() {
492492
assertThat(properties.getStringProperty("spring")).isEqualTo("boot");
493493
}
494494

495+
@Test
496+
void environmentIsUpdatedUponReinitialization() {
497+
MockEnvironment environment = new MockEnvironment();
498+
environment.setProperty("spring", "boot: one");
499+
this.loggingSystem.beforeInitialize();
500+
this.loggingSystem.initialize(new LoggingInitializationContext(environment), null, null);
501+
assertThat(PropertiesUtil.getProperties().getStringProperty("spring")).isEqualTo("boot: one");
502+
this.loggingSystem.cleanUp();
503+
this.environment.setProperty("spring", "boot: two");
504+
this.loggingSystem.beforeInitialize();
505+
this.loggingSystem.initialize(this.initializationContext, null, null);
506+
assertThat(PropertiesUtil.getProperties().getStringProperty("spring")).isEqualTo("boot: two");
507+
}
508+
495509
@Test
496510
void nonFileUrlsAreResolvedUsingLog4J2UrlConnectionFactory() {
497511
this.loggingSystem.beforeInitialize();

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

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,7 +26,6 @@
2626
import org.springframework.mock.env.MockEnvironment;
2727

2828
import static org.assertj.core.api.Assertions.assertThat;
29-
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
3029

3130
/**
3231
* Tests for {@link SpringEnvironmentPropertySource}.
@@ -43,13 +42,8 @@ class SpringEnvironmentPropertySourceTests {
4342
void setup() {
4443
this.environment = new MockEnvironment();
4544
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");
45+
this.propertySource = new SpringEnvironmentPropertySource();
46+
this.propertySource.setEnvironment(this.environment);
5347
}
5448

5549
@Test
@@ -65,6 +59,12 @@ void getPropertyWhenInEnvironmentReturnsValue() {
6559
assertThat(this.propertySource.getProperty("spring")).isEqualTo("boot");
6660
}
6761

62+
@Test
63+
void getPropertyWhenEnvironmentIsNullReturnsNull() {
64+
this.propertySource.setEnvironment(null);
65+
assertThat(this.propertySource.getProperty("spring")).isNull();
66+
}
67+
6868
@Test
6969
void getPropertyWhenNotInEnvironmentReturnsNull() {
7070
assertThat(this.propertySource.getProperty("nope")).isNull();
@@ -75,6 +75,12 @@ void containsPropertyWhenInEnvironmentReturnsTrue() {
7575
assertThat(this.propertySource.containsProperty("spring")).isTrue();
7676
}
7777

78+
@Test
79+
void containsPropertyWhenEnvironmentIsNullReturnsFalse() {
80+
this.propertySource.setEnvironment(null);
81+
assertThat(this.propertySource.containsProperty("spring")).isFalse();
82+
}
83+
7884
@Test
7985
void containsPropertyWhenNotInEnvironmentReturnsFalse() {
8086
assertThat(this.propertySource.containsProperty("nope")).isFalse();

0 commit comments

Comments
 (0)