Browse Source

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 10 years ago
parent
commit
2cb38bc8e2
  1. 8
      spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java
  2. 7
      spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java

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

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

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

@ -209,6 +209,13 @@ public class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests { @@ -209,6 +209,13 @@ public class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
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
public void customExceptionConversionWord() throws Exception {
System.setProperty("LOG_EXCEPTION_CONVERSION_WORD", "%ex");

Loading…
Cancel
Save