Skip to content

Commit 838e0ef

Browse files
committed
Polish contribution
- Extract the logic that coerces the string into a LogLevel into a separate method. - Add a test that verifies that false is mapped to LogLevel.OFF Closes gh-3628
1 parent cbd37b5 commit 838e0ef

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -246,18 +246,20 @@ private void setLogLevel(LoggingSystem system, Environment environment, String n
246246
name = null;
247247
}
248248
level = environment.resolvePlaceholders(level);
249-
if (Boolean.toString(false).equalsIgnoreCase(level)) {
250-
system.setLogLevel(name, LogLevel.OFF);
251-
}
252-
else {
253-
system.setLogLevel(name, LogLevel.valueOf(level.toUpperCase()));
254-
}
249+
system.setLogLevel(name, coerceLogLevel(level));
255250
}
256251
catch (RuntimeException ex) {
257252
this.logger.error("Cannot set level: " + level + " for '" + name + "'");
258253
}
259254
}
260255

256+
private LogLevel coerceLogLevel(String level) {
257+
if ("false".equalsIgnoreCase(level)) {
258+
return LogLevel.OFF;
259+
}
260+
return LogLevel.valueOf(level.toUpperCase());
261+
}
262+
261263
public void setOrder(int order) {
262264
this.order = order;
263265
}

spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,18 @@ public void parseLevelsNone() throws Exception {
258258
assertThat(this.outputCapture.toString(), not(containsString("testatfatal")));
259259
}
260260

261+
@Test
262+
public void parseLevelsMapsFalseToOff() throws Exception {
263+
EnvironmentTestUtils.addEnvironment(this.context,
264+
"logging.level.org.springframework.boot=false");
265+
this.initializer.initialize(this.context.getEnvironment(),
266+
this.context.getClassLoader());
267+
this.logger.debug("testatdebug");
268+
this.logger.fatal("testatfatal");
269+
assertThat(this.outputCapture.toString(), not(containsString("testatdebug")));
270+
assertThat(this.outputCapture.toString(), not(containsString("testatfatal")));
271+
}
272+
261273
@Test
262274
public void parseArgsDisabled() throws Exception {
263275
this.initializer.setParseArgs(false);

0 commit comments

Comments
 (0)