Commit 74740c3b authored by Andy Wilkinson's avatar Andy Wilkinson

Merge pull request #15143 from Dileep Bapat

* gh-15143:
  Polish "Trim whitespace when coercing to a LogLevel"
  Trim whitespace when coercing to a LogLevel
parents d6571c01 e1ad5641
...@@ -367,15 +367,16 @@ public class LoggingApplicationListener implements GenericApplicationListener { ...@@ -367,15 +367,16 @@ public class LoggingApplicationListener implements GenericApplicationListener {
system.setLogLevel(name, coerceLogLevel(level)); system.setLogLevel(name, coerceLogLevel(level));
} }
catch (RuntimeException ex) { 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) { private LogLevel coerceLogLevel(String level) {
if ("false".equalsIgnoreCase(level)) { String trimmedLevel = level.trim();
if ("false".equalsIgnoreCase(trimmedLevel)) {
return LogLevel.OFF; return LogLevel.OFF;
} }
return LogLevel.valueOf(level.toUpperCase(Locale.ENGLISH)); return LogLevel.valueOf(trimmedLevel.toUpperCase(Locale.ENGLISH));
} }
private void registerShutdownHookIfNecessary(Environment environment, private void registerShutdownHookIfNecessary(Environment environment,
......
...@@ -318,6 +318,18 @@ public class LoggingApplicationListenerTests { ...@@ -318,6 +318,18 @@ public class LoggingApplicationListenerTests {
assertThat(this.outputCapture.toString()).contains("testattrace"); 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 @Test
public void parseLevelsWithPlaceholder() { public void parseLevelsWithPlaceholder() {
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
...@@ -338,7 +350,7 @@ public class LoggingApplicationListenerTests { ...@@ -338,7 +350,7 @@ public class LoggingApplicationListenerTests {
this.context.getClassLoader()); this.context.getClassLoader());
this.logger.debug("testatdebug"); this.logger.debug("testatdebug");
assertThat(this.outputCapture.toString()).doesNotContain("testatdebug") assertThat(this.outputCapture.toString()).doesNotContain("testatdebug")
.contains("Cannot set level: GARBAGE"); .contains("Cannot set level 'GARBAGE'");
} }
@Test @Test
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment