Skip to content

Commit 2cb38bc

Browse files
committed
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
1 parent 3c67ecc commit 2cb38bc

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,13 @@ protected boolean isClassAvailable(String className) {
130130
@Override
131131
public void beforeInitialize() {
132132
super.beforeInitialize();
133-
getRootLoggerConfig().addFilter(FILTER);
133+
getLoggerContext().getConfiguration().addFilter(FILTER);
134134
}
135135

136136
@Override
137137
public void initialize(LoggingInitializationContext initializationContext,
138138
String configLocation, LogFile logFile) {
139-
getRootLoggerConfig().removeFilter(FILTER);
139+
getLoggerContext().getConfiguration().removeFilter(FILTER);
140140
super.initialize(initializationContext, configLocation, logFile);
141141
}
142142

@@ -204,10 +204,6 @@ public Runnable getShutdownHandler() {
204204
return new ShutdownHandler();
205205
}
206206

207-
private LoggerConfig getRootLoggerConfig() {
208-
return getLoggerContext().getConfiguration().getLoggerConfig("");
209-
}
210-
211207
private LoggerConfig getLoggerConfig(String name) {
212208
name = (StringUtils.hasText(name) ? name : LogManager.ROOT_LOGGER_NAME);
213209
return getLoggerContext().getConfiguration().getLoggers().get(name);

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,13 @@ public void exceptionsIncludeClassPackaging() throws Exception {
209209
assertThat(fileContents, is(expectedOutput));
210210
}
211211

212+
@Test
213+
public void beforeInitializeFilterDisablesErrorLogging() throws Exception {
214+
this.loggingSystem.beforeInitialize();
215+
assertFalse(this.logger.isErrorEnabled());
216+
this.loggingSystem.initialize(null, null, getLogFile(null, tmpDir()));
217+
}
218+
212219
@Test
213220
public void customExceptionConversionWord() throws Exception {
214221
System.setProperty("LOG_EXCEPTION_CONVERSION_WORD", "%ex");

0 commit comments

Comments
 (0)