From cbd37b583ff00f33553e92249b3ece3fa543f675 Mon Sep 17 00:00:00 2001 From: shanman190 Date: Thu, 30 Jul 2015 16:37:35 -0500 Subject: [PATCH 1/2] Make it easier to use YAML configuration to turn off a logger A level named off is used to disable logging for a particular logger. YAML interprets off as false, leading to a failed attempt to get the LogLevel for FALSE. A workaround is to quote the level, i.e. use "off" rather than off. This commit updates LoggingApplicationListener to coerce the string false back to the level off. Closes gh-3631 See gh-3628 --- .../boot/logging/LoggingApplicationListener.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java index 6c7bfa4464..4d6873b844 100644 --- a/spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java @@ -246,7 +246,12 @@ public class LoggingApplicationListener implements SmartApplicationListener { name = null; } level = environment.resolvePlaceholders(level); - system.setLogLevel(name, LogLevel.valueOf(level.toUpperCase())); + if (Boolean.toString(false).equalsIgnoreCase(level)) { + system.setLogLevel(name, LogLevel.OFF); + } + else { + system.setLogLevel(name, LogLevel.valueOf(level.toUpperCase())); + } } catch (RuntimeException ex) { this.logger.error("Cannot set level: " + level + " for '" + name + "'"); From 838e0ef33e6b8596b21984c63085896b7ef75e2b Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 3 Aug 2015 16:18:07 +0100 Subject: [PATCH 2/2] 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 --- .../boot/logging/LoggingApplicationListener.java | 14 ++++++++------ .../logging/LoggingApplicationListenerTests.java | 12 ++++++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java index 4d6873b844..958a553e65 100644 --- a/spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java @@ -246,18 +246,20 @@ public class LoggingApplicationListener implements SmartApplicationListener { name = null; } level = environment.resolvePlaceholders(level); - if (Boolean.toString(false).equalsIgnoreCase(level)) { - system.setLogLevel(name, LogLevel.OFF); - } - else { - system.setLogLevel(name, LogLevel.valueOf(level.toUpperCase())); - } + system.setLogLevel(name, coerceLogLevel(level)); } catch (RuntimeException ex) { this.logger.error("Cannot set level: " + level + " for '" + name + "'"); } } + private LogLevel coerceLogLevel(String level) { + if ("false".equalsIgnoreCase(level)) { + return LogLevel.OFF; + } + return LogLevel.valueOf(level.toUpperCase()); + } + public void setOrder(int order) { this.order = order; } diff --git a/spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerTests.java b/spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerTests.java index 7eceeb3dbb..e413306793 100644 --- a/spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerTests.java @@ -258,6 +258,18 @@ public class LoggingApplicationListenerTests { assertThat(this.outputCapture.toString(), not(containsString("testatfatal"))); } + @Test + public void parseLevelsMapsFalseToOff() throws Exception { + EnvironmentTestUtils.addEnvironment(this.context, + "logging.level.org.springframework.boot=false"); + this.initializer.initialize(this.context.getEnvironment(), + this.context.getClassLoader()); + this.logger.debug("testatdebug"); + this.logger.fatal("testatfatal"); + assertThat(this.outputCapture.toString(), not(containsString("testatdebug"))); + assertThat(this.outputCapture.toString(), not(containsString("testatfatal"))); + } + @Test public void parseArgsDisabled() throws Exception { this.initializer.setParseArgs(false);