From d22265b13e848be42d8f0b0e3adc2996895a337c Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 11 Mar 2016 11:27:39 +0100 Subject: [PATCH] Allow to disable debug property Previously, adding `debug=false` in the environment had no effect as the mere presence of the property was used to enable the debug mode. This commit makes sure to also check the value and ignore the property if it is set to `false`. The documentation has also been updated to refer to the `trace` property. Closes gh-5374 --- .../main/asciidoc/spring-boot-features.adoc | 7 ++++++- .../logging/LoggingApplicationListener.java | 9 ++++++-- ...itional-spring-configuration-metadata.json | 7 +++++++ .../LoggingApplicationListenerTests.java | 21 +++++++++++++++++++ 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index aace21c061b..c8e2aa66c70 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -1072,9 +1072,14 @@ default `ERROR`, `WARN` and `INFO` level messages are logged. You can also enabl NOTE: you can also specify `debug=true` in your `application.properties`. When the debug mode is enabled, a selection of core loggers (embedded container, Hibernate -and Spring) are configured to output more information. Enabling the debug mode does _not_ +and Spring Boot) are configured to output more information. Enabling the debug mode does _not_ configure your application to log all messages with `DEBUG` level. +Alternatively, you can enable a "`trace`" mode by starting your application with a `--trace` +flag (or `trace=true` in your `application.properties`). This will enable trace logging for a +selection of core loggers (embedded container, Hibernate schema generation and the whole Spring +portfolio). + [[boot-features-logging-color-coded-output]] ==== Color-coded output If your terminal supports ANSI, color output will be used to aid readability. You can set 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 413740fbc40..537bbed3448 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 @@ -284,15 +284,20 @@ public class LoggingApplicationListener implements GenericApplicationListener { private void initializeEarlyLoggingLevel(ConfigurableEnvironment environment) { if (this.parseArgs && this.springBootLogging == null) { - if (environment.containsProperty("debug")) { + if (isSet(environment, "debug")) { this.springBootLogging = LogLevel.DEBUG; } - if (environment.containsProperty("trace")) { + if (isSet(environment, "trace")) { this.springBootLogging = LogLevel.TRACE; } } } + private boolean isSet(ConfigurableEnvironment environment, String property) { + String value = environment.getProperty(property); + return !(value == null || value.equals("false")); + } + private void initializeSystem(ConfigurableEnvironment environment, LoggingSystem system, LogFile logFile) { LoggingInitializationContext initializationContext = new LoggingInitializationContext( diff --git a/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json index bd482821348..80a1d91811a 100644 --- a/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -160,6 +160,13 @@ "type": "java.lang.String", "sourceType": "org.springframework.boot.context.config.ConfigFileApplicationListener", "description": "Unconditionally activate the specified comma separated profiles." + }, + { + "name": "trace", + "type": "java.lang.Boolean", + "description": "Enable trace logs.", + "sourceType": "org.springframework.boot.logging.LoggingApplicationListener", + "defaultValue": false } ],"hints": [ { 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 a908cd98449..419cbf6128d 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 @@ -55,6 +55,7 @@ import static org.hamcrest.Matchers.not; * @author Dave Syer * @author Phillip Webb * @author Andy Wilkinson + * @author Stephane Nicoll */ public class LoggingApplicationListenerTests { @@ -231,6 +232,26 @@ public class LoggingApplicationListenerTests { assertThat(this.outputCapture.toString()).contains("testattrace"); } + @Test + public void disableDebugArg() { + disableDebugTraceArg("debug=false"); + } + + @Test + public void disableTraceArg() { + disableDebugTraceArg("trace=false"); + } + + private void disableDebugTraceArg(String... environment) { + EnvironmentTestUtils.addEnvironment(this.context, environment); + this.initializer.initialize(this.context.getEnvironment(), + this.context.getClassLoader()); + this.logger.debug("testatdebug"); + this.logger.trace("testattrace"); + assertThat(this.outputCapture.toString()).doesNotContain("testatdebug"); + assertThat(this.outputCapture.toString()).doesNotContain("testattrace"); + } + @Test public void parseLevels() throws Exception { EnvironmentTestUtils.addEnvironment(this.context,