Polish "Trim whitespace when coercing to a LogLevel"

Closes gh-15143
This commit is contained in:
Andy Wilkinson 2018-11-27 15:10:04 +00:00
parent dfe94a0abb
commit e1ad5641d5
2 changed files with 18 additions and 5 deletions

View File

@ -364,18 +364,19 @@ public class LoggingApplicationListener implements GenericApplicationListener {
private void setLogLevel(LoggingSystem system, String name, String level) {
try {
name = name.equalsIgnoreCase(LoggingSystem.ROOT_LOGGER_NAME) ? null : name;
system.setLogLevel(name, coerceLogLevel(level.trim()));
system.setLogLevel(name, coerceLogLevel(level));
}
catch (RuntimeException ex) {
this.logger.error("Cannot set level: " + level + " for '" + name + "'");
this.logger.error("Cannot set level '" + level + "' for '" + name + "'");
}
}
private LogLevel coerceLogLevel(String level) {
if ("false".equalsIgnoreCase(level)) {
String trimmedLevel = level.trim();
if ("false".equalsIgnoreCase(trimmedLevel)) {
return LogLevel.OFF;
}
return LogLevel.valueOf(level.toUpperCase(Locale.ENGLISH));
return LogLevel.valueOf(trimmedLevel.toUpperCase(Locale.ENGLISH));
}
private void registerShutdownHookIfNecessary(Environment environment,

View File

@ -318,6 +318,18 @@ public class LoggingApplicationListenerTests {
assertThat(this.outputCapture.toString()).contains("testattrace");
}
@Test
public void parseLevelsTrimsWhitespace() {
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"logging.level.org.springframework.boot= trace ");
this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader());
this.logger.debug("testatdebug");
this.logger.trace("testattrace");
assertThat(this.outputCapture.toString()).contains("testatdebug");
assertThat(this.outputCapture.toString()).contains("testattrace");
}
@Test
public void parseLevelsWithPlaceholder() {
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
@ -338,7 +350,7 @@ public class LoggingApplicationListenerTests {
this.context.getClassLoader());
this.logger.debug("testatdebug");
assertThat(this.outputCapture.toString()).doesNotContain("testatdebug")
.contains("Cannot set level: GARBAGE");
.contains("Cannot set level 'GARBAGE'");
}
@Test