Skip to content

Commit

Permalink
Merge branch '3.2.x'
Browse files Browse the repository at this point in the history
Closes gh-40327
  • Loading branch information
wilkinsona committed Apr 12, 2024
2 parents 04faec1 + 1c5ba19 commit a3d62e0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ public class Log4J2LoggingSystem extends AbstractLoggingSystem {

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

private static final SpringEnvironmentPropertySource propertySource = new SpringEnvironmentPropertySource();

static final String ENVIRONMENT_KEY = Conventions.getQualifiedAttributeName(Log4J2LoggingSystem.class,
"environment");

Expand Down Expand Up @@ -215,8 +217,9 @@ public void initialize(LoggingInitializationContext initializationContext, Strin
}
Environment environment = initializationContext.getEnvironment();
if (environment != null) {
getLoggerContext().putObjectIfAbsent(ENVIRONMENT_KEY, environment);
PropertiesUtil.getProperties().addPropertySource(new SpringEnvironmentPropertySource(environment));
getLoggerContext().putObject(ENVIRONMENT_KEY, environment);
Log4J2LoggingSystem.propertySource.setEnvironment(environment);
PropertiesUtil.getProperties().addPropertySource(Log4J2LoggingSystem.propertySource);
}
loggerContext.getConfiguration().removeFilter(FILTER);
super.initialize(initializationContext, configLocation, logFile);
Expand Down Expand Up @@ -439,6 +442,8 @@ public void cleanUp() {
LoggerContext loggerContext = getLoggerContext();
markAsUninitialized(loggerContext);
loggerContext.getConfiguration().removeFilter(FILTER);
Log4J2LoggingSystem.propertySource.setEnvironment(null);
getLoggerContext().removeObject(ENVIRONMENT_KEY);
}

private LoggerConfig getLogger(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.apache.logging.log4j.util.PropertySource;

import org.springframework.core.env.Environment;
import org.springframework.util.Assert;

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

private final Environment environment;

SpringEnvironmentPropertySource(Environment environment) {
Assert.notNull(environment, "Environment must not be null");
this.environment = environment;
}
private volatile Environment environment;

@Override
public int getPriority() {
Expand All @@ -47,12 +41,18 @@ public int getPriority() {

@Override
public String getProperty(String key) {
return this.environment.getProperty(key);
Environment environment = this.environment;
return (environment != null) ? environment.getProperty(key) : null;
}

@Override
public boolean containsProperty(String key) {
return this.environment.containsProperty(key);
Environment environment = this.environment;
return (environment != null) ? environment.containsProperty(key) : false;
}

void setEnvironment(Environment environment) {
this.environment = environment;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,20 @@ void initializeAddsSpringEnvironmentPropertySource() {
assertThat(properties.getStringProperty("spring")).isEqualTo("boot");
}

@Test
void environmentIsUpdatedUponReinitialization() {
MockEnvironment environment = new MockEnvironment();
environment.setProperty("spring", "boot: one");
this.loggingSystem.beforeInitialize();
this.loggingSystem.initialize(new LoggingInitializationContext(environment), null, null);
assertThat(PropertiesUtil.getProperties().getStringProperty("spring")).isEqualTo("boot: one");
this.loggingSystem.cleanUp();
this.environment.setProperty("spring", "boot: two");
this.loggingSystem.beforeInitialize();
this.loggingSystem.initialize(this.initializationContext, null, null);
assertThat(PropertiesUtil.getProperties().getStringProperty("spring")).isEqualTo("boot: two");
}

@Test
void nonFileUrlsAreResolvedUsingLog4J2UrlConnectionFactory() {
this.loggingSystem.beforeInitialize();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,7 +26,6 @@
import org.springframework.mock.env.MockEnvironment;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;

/**
* Tests for {@link SpringEnvironmentPropertySource}.
Expand All @@ -43,13 +42,8 @@ class SpringEnvironmentPropertySourceTests {
void setup() {
this.environment = new MockEnvironment();
this.environment.setProperty("spring", "boot");
this.propertySource = new SpringEnvironmentPropertySource(this.environment);
}

@Test
void createWhenEnvironmentIsNullThrowsException() {
assertThatIllegalArgumentException().isThrownBy(() -> new SpringEnvironmentPropertySource(null))
.withMessage("Environment must not be null");
this.propertySource = new SpringEnvironmentPropertySource();
this.propertySource.setEnvironment(this.environment);
}

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

@Test
void getPropertyWhenEnvironmentIsNullReturnsNull() {
this.propertySource.setEnvironment(null);
assertThat(this.propertySource.getProperty("spring")).isNull();
}

@Test
void getPropertyWhenNotInEnvironmentReturnsNull() {
assertThat(this.propertySource.getProperty("nope")).isNull();
Expand All @@ -75,6 +75,12 @@ void containsPropertyWhenInEnvironmentReturnsTrue() {
assertThat(this.propertySource.containsProperty("spring")).isTrue();
}

@Test
void containsPropertyWhenEnvironmentIsNullReturnsFalse() {
this.propertySource.setEnvironment(null);
assertThat(this.propertySource.containsProperty("spring")).isFalse();
}

@Test
void containsPropertyWhenNotInEnvironmentReturnsFalse() {
assertThat(this.propertySource.containsProperty("nope")).isFalse();
Expand Down

0 comments on commit a3d62e0

Please sign in to comment.