Skip to content

Commit a43cd18

Browse files
committed
Avoid changing root logger level when setting level of unconfigured logger
Previously getLoggerConfig(loggerName) was used to retrieve the LoggerConfig object on which the level was to be set. As described in the method’s javadoc it will “remove tokens from the package name as necessary or return the root LoggerConfig if no other matches were found”. This is problematic as, if there’s no configuration for the logger whose level is being configured, the level will be applied to a logger from an outer package or to the root logger. This commit updates Log4J2LoggingSystem to use the configuration’s map of LoggerConfigs, rather than calling getLoggerConfig. In the event of the level being set on an unconfigured logger, this will produce a null LoggerConfig. When a null LoggerConfig is encountered, a new one is created with the appropriate level. If the config already exists, its level is set as it was before. The code that was accessing the root logger using a magic null value (which was then coerced into the root logger’s name (an empty string)) has also been updated to make it clearer that they are purposefully dealing with the root logger. Closes gh-3550
1 parent 9b6538d commit a43cd18

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,12 @@ protected boolean isClassAvailable(String className) {
121121
@Override
122122
public void beforeInitialize() {
123123
super.beforeInitialize();
124-
getLoggerConfig(null).addFilter(FILTER);
124+
getRootLoggerConfig().addFilter(FILTER);
125125
}
126126

127127
@Override
128128
public void initialize(String configLocation, LogFile logFile) {
129-
getLoggerConfig(null).removeFilter(FILTER);
129+
getRootLoggerConfig().removeFilter(FILTER);
130130
super.initialize(configLocation, logFile);
131131
}
132132

@@ -164,15 +164,25 @@ protected void reinitialize() {
164164
}
165165

166166
@Override
167-
public void setLogLevel(String loggerName, LogLevel level) {
168-
getLoggerConfig(loggerName).setLevel(LEVELS.get(level));
167+
public void setLogLevel(String loggerName, LogLevel logLevel) {
168+
Level level = LEVELS.get(logLevel);
169+
LoggerConfig loggerConfig = getLoggerConfig(loggerName);
170+
if (loggerConfig == null) {
171+
loggerConfig = new LoggerConfig(loggerName, level, true);
172+
getLoggerContext().getConfiguration().addLogger(loggerName, loggerConfig);
173+
}
174+
else {
175+
loggerConfig.setLevel(level);
176+
}
169177
getLoggerContext().updateLoggers();
170178
}
171179

180+
private LoggerConfig getRootLoggerConfig() {
181+
return getLoggerContext().getConfiguration().getLoggerConfig("");
182+
}
183+
172184
private LoggerConfig getLoggerConfig(String loggerName) {
173-
LoggerConfig loggerConfig = getLoggerContext().getConfiguration()
174-
.getLoggerConfig(loggerName == null ? "" : loggerName);
175-
return loggerConfig;
185+
return getLoggerContext().getConfiguration().getLoggers().get(loggerName);
176186
}
177187

178188
private LoggerContext getLoggerContext() {

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@
3434

3535
import com.fasterxml.jackson.databind.ObjectMapper;
3636

37+
import static org.hamcrest.CoreMatchers.containsString;
3738
import static org.hamcrest.Matchers.arrayContaining;
3839
import static org.hamcrest.Matchers.equalTo;
3940
import static org.hamcrest.Matchers.is;
41+
import static org.hamcrest.Matchers.not;
4042
import static org.junit.Assert.assertFalse;
4143
import static org.junit.Assert.assertThat;
4244
import static org.junit.Assert.assertTrue;
@@ -115,6 +117,17 @@ public void setLevel() throws Exception {
115117
equalTo(1));
116118
}
117119

120+
@Test
121+
public void setLevelOfUnconfiguredLoggerDoesNotAffectRootConfiguration()
122+
throws Exception {
123+
this.loggingSystem.beforeInitialize();
124+
this.loggingSystem.initialize(null, null);
125+
LogManager.getRootLogger().debug("Hello");
126+
this.loggingSystem.setLogLevel("foo.bar.baz", LogLevel.DEBUG);
127+
LogManager.getRootLogger().debug("Hello");
128+
assertThat(this.output.toString(), not(containsString("Hello")));
129+
}
130+
118131
@Test
119132
@Ignore("Fails on Bamboo")
120133
public void loggingThatUsesJulIsCaptured() {

0 commit comments

Comments
 (0)