Apply Log4J2LoggingSystem.FILTER to main config

Update Log4J2LoggingSystem so that the FILTER is applied to the main
configuration and not to the root logger. Prior to this commit calls
to `logger.isErrorEnabled()` would not consider the filter and hence
would always return `true`. This caused `SpringApplication` to silently
swallow exceptions.

Fixes gh-5271
pull/6458/head
Phillip Webb 8 years ago
parent 3c67ecca61
commit 2cb38bc8e2

@ -130,13 +130,13 @@ public class Log4J2LoggingSystem extends Slf4JLoggingSystem {
@Override @Override
public void beforeInitialize() { public void beforeInitialize() {
super.beforeInitialize(); super.beforeInitialize();
getRootLoggerConfig().addFilter(FILTER); getLoggerContext().getConfiguration().addFilter(FILTER);
} }
@Override @Override
public void initialize(LoggingInitializationContext initializationContext, public void initialize(LoggingInitializationContext initializationContext,
String configLocation, LogFile logFile) { String configLocation, LogFile logFile) {
getRootLoggerConfig().removeFilter(FILTER); getLoggerContext().getConfiguration().removeFilter(FILTER);
super.initialize(initializationContext, configLocation, logFile); super.initialize(initializationContext, configLocation, logFile);
} }
@ -204,10 +204,6 @@ public class Log4J2LoggingSystem extends Slf4JLoggingSystem {
return new ShutdownHandler(); return new ShutdownHandler();
} }
private LoggerConfig getRootLoggerConfig() {
return getLoggerContext().getConfiguration().getLoggerConfig("");
}
private LoggerConfig getLoggerConfig(String name) { private LoggerConfig getLoggerConfig(String name) {
name = (StringUtils.hasText(name) ? name : LogManager.ROOT_LOGGER_NAME); name = (StringUtils.hasText(name) ? name : LogManager.ROOT_LOGGER_NAME);
return getLoggerContext().getConfiguration().getLoggers().get(name); return getLoggerContext().getConfiguration().getLoggers().get(name);

@ -209,6 +209,13 @@ public class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
assertThat(fileContents, is(expectedOutput)); assertThat(fileContents, is(expectedOutput));
} }
@Test
public void beforeInitializeFilterDisablesErrorLogging() throws Exception {
this.loggingSystem.beforeInitialize();
assertFalse(this.logger.isErrorEnabled());
this.loggingSystem.initialize(null, null, getLogFile(null, tmpDir()));
}
@Test @Test
public void customExceptionConversionWord() throws Exception { public void customExceptionConversionWord() throws Exception {
System.setProperty("LOG_EXCEPTION_CONVERSION_WORD", "%ex"); System.setProperty("LOG_EXCEPTION_CONVERSION_WORD", "%ex");

Loading…
Cancel
Save