Skip to content

Commit d22265b

Browse files
committed
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
1 parent 8cb602f commit d22265b

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1072,9 +1072,14 @@ default `ERROR`, `WARN` and `INFO` level messages are logged. You can also enabl
10721072
NOTE: you can also specify `debug=true` in your `application.properties`.
10731073

10741074
When the debug mode is enabled, a selection of core loggers (embedded container, Hibernate
1075-
and Spring) are configured to output more information. Enabling the debug mode does _not_
1075+
and Spring Boot) are configured to output more information. Enabling the debug mode does _not_
10761076
configure your application to log all messages with `DEBUG` level.
10771077

1078+
Alternatively, you can enable a "`trace`" mode by starting your application with a `--trace`
1079+
flag (or `trace=true` in your `application.properties`). This will enable trace logging for a
1080+
selection of core loggers (embedded container, Hibernate schema generation and the whole Spring
1081+
portfolio).
1082+
10781083
[[boot-features-logging-color-coded-output]]
10791084
==== Color-coded output
10801085
If your terminal supports ANSI, color output will be used to aid readability. You can set

spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,15 +284,20 @@ private void setSystemProperty(String name, String value) {
284284

285285
private void initializeEarlyLoggingLevel(ConfigurableEnvironment environment) {
286286
if (this.parseArgs && this.springBootLogging == null) {
287-
if (environment.containsProperty("debug")) {
287+
if (isSet(environment, "debug")) {
288288
this.springBootLogging = LogLevel.DEBUG;
289289
}
290-
if (environment.containsProperty("trace")) {
290+
if (isSet(environment, "trace")) {
291291
this.springBootLogging = LogLevel.TRACE;
292292
}
293293
}
294294
}
295295

296+
private boolean isSet(ConfigurableEnvironment environment, String property) {
297+
String value = environment.getProperty(property);
298+
return !(value == null || value.equals("false"));
299+
}
300+
296301
private void initializeSystem(ConfigurableEnvironment environment,
297302
LoggingSystem system, LogFile logFile) {
298303
LoggingInitializationContext initializationContext = new LoggingInitializationContext(

spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,13 @@
160160
"type": "java.lang.String",
161161
"sourceType": "org.springframework.boot.context.config.ConfigFileApplicationListener",
162162
"description": "Unconditionally activate the specified comma separated profiles."
163+
},
164+
{
165+
"name": "trace",
166+
"type": "java.lang.Boolean",
167+
"description": "Enable trace logs.",
168+
"sourceType": "org.springframework.boot.logging.LoggingApplicationListener",
169+
"defaultValue": false
163170
}
164171
],"hints": [
165172
{

spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
* @author Dave Syer
5656
* @author Phillip Webb
5757
* @author Andy Wilkinson
58+
* @author Stephane Nicoll
5859
*/
5960
public class LoggingApplicationListenerTests {
6061

@@ -231,6 +232,26 @@ public void parseTraceArg() throws Exception {
231232
assertThat(this.outputCapture.toString()).contains("testattrace");
232233
}
233234

235+
@Test
236+
public void disableDebugArg() {
237+
disableDebugTraceArg("debug=false");
238+
}
239+
240+
@Test
241+
public void disableTraceArg() {
242+
disableDebugTraceArg("trace=false");
243+
}
244+
245+
private void disableDebugTraceArg(String... environment) {
246+
EnvironmentTestUtils.addEnvironment(this.context, environment);
247+
this.initializer.initialize(this.context.getEnvironment(),
248+
this.context.getClassLoader());
249+
this.logger.debug("testatdebug");
250+
this.logger.trace("testattrace");
251+
assertThat(this.outputCapture.toString()).doesNotContain("testatdebug");
252+
assertThat(this.outputCapture.toString()).doesNotContain("testattrace");
253+
}
254+
234255
@Test
235256
public void parseLevels() throws Exception {
236257
EnvironmentTestUtils.addEnvironment(this.context,

0 commit comments

Comments
 (0)