Merge pull request for #32730 from rgoers

Closes gh-32731
pull/32740/head
Phillip Webb 2 years ago
commit 66e097e427

@ -58,8 +58,10 @@ import org.springframework.boot.logging.LoggerConfiguration;
import org.springframework.boot.logging.LoggingInitializationContext;
import org.springframework.boot.logging.LoggingSystem;
import org.springframework.boot.logging.LoggingSystemFactory;
import org.springframework.core.Conventions;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils;
@ -84,6 +86,9 @@ public class Log4J2LoggingSystem extends AbstractLoggingSystem {
private static final String LOG4J_LOG_MANAGER = "org.apache.logging.log4j.jul.LogManager";
static final String ENVIRONMENT_KEY = Conventions.getQualifiedAttributeName(Log4J2LoggingSystem.class,
"environment");
private static final LogLevels<Level> LEVELS = new LogLevels<>();
static {
@ -227,6 +232,8 @@ public class Log4J2LoggingSystem extends AbstractLoggingSystem {
if (isAlreadyInitialized(loggerContext)) {
return;
}
Environment environment = initializationContext.getEnvironment();
getLoggerContext().putObjectIfAbsent(ENVIRONMENT_KEY, environment);
loggerContext.getConfiguration().removeFilter(FILTER);
super.initialize(initializationContext, configLocation, logFile);
markAsInitialized(loggerContext);
@ -467,6 +474,17 @@ public class Log4J2LoggingSystem extends AbstractLoggingSystem {
loggerContext.setExternalContext(null);
}
/**
* Get the Spring {@link Environment} attached to the given {@link LoggerContext} or
* {@code null} if no environment is available.
* @param loggerContext the logger context
* @return the Spring {@link Environment} or {@code null}
* @since 3.0.0
*/
public static Environment getEnvironment(LoggerContext loggerContext) {
return (Environment) ((loggerContext != null) ? loggerContext.getObject(ENVIRONMENT_KEY) : null);
}
/**
* {@link LoggingSystemFactory} that returns {@link Log4J2LoggingSystem} if possible.
*/

@ -57,6 +57,7 @@ import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
import org.springframework.boot.testsupport.logging.ConfigureClasspathToPreferLog4j2;
import org.springframework.boot.testsupport.system.CapturedOutput;
import org.springframework.boot.testsupport.system.OutputCaptureExtension;
import org.springframework.core.env.Environment;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
@ -438,6 +439,15 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
assertThat(this.loggingSystem.getConfiguration()).isInstanceOf(CompositeConfiguration.class);
}
@Test
void initializeAttachesEnvironmentToLoggerContext() {
this.loggingSystem.beforeInitialize();
this.loggingSystem.initialize(this.initializationContext, null, null);
LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
Environment environment = Log4J2LoggingSystem.getEnvironment(loggerContext);
assertThat(environment).isSameAs(this.environment);
}
private String getRelativeClasspathLocation(String fileName) {
String defaultPath = ClassUtils.getPackageName(getClass());
defaultPath = defaultPath.replace('.', '/');

Loading…
Cancel
Save