Polish 'Add Spring Environment to LoggerContext'
See gh-32731
This commit is contained in:
parent
05a2bd4585
commit
d665441ca9
|
|
@ -58,6 +58,7 @@ 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;
|
||||
|
|
@ -85,10 +86,8 @@ public class Log4J2LoggingSystem extends AbstractLoggingSystem {
|
|||
|
||||
private static final String LOG4J_LOG_MANAGER = "org.apache.logging.log4j.jul.LogManager";
|
||||
|
||||
/**
|
||||
* Identifies the Spring environment.
|
||||
*/
|
||||
public static final String ENVIRONMENT_KEY = "SpringEnvironment";
|
||||
static final String ENVIRONMENT_KEY = Conventions.getQualifiedAttributeName(Log4J2LoggingSystem.class,
|
||||
"environment");
|
||||
|
||||
private static final LogLevels<Level> LEVELS = new LogLevels<>();
|
||||
|
||||
|
|
@ -475,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…
Reference in New Issue