From e1ad5641d5f8c37b42e66a436fb470fe52049697 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 27 Nov 2018 15:10:04 +0000 Subject: [PATCH] Polish "Trim whitespace when coercing to a LogLevel" Closes gh-15143 --- .../logging/LoggingApplicationListener.java | 9 +++++---- .../logging/LoggingApplicationListenerTests.java | 14 +++++++++++++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/logging/LoggingApplicationListener.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/logging/LoggingApplicationListener.java index 781297c12f..ac2854b9dd 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/logging/LoggingApplicationListener.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/logging/LoggingApplicationListener.java @@ -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, diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerTests.java index a95ec3f26e..8f5cafa3ac 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerTests.java @@ -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