From ac2e6972d7ca7f19cc326d2042e35112d8b337d8 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 20 Aug 2025 19:17:30 -0700 Subject: [PATCH] Provide configuration property to disable console logging Add `logging.console.enabled` which when set will cause the `logging.threshold.console` property to be set to `OFF`. Closes gh-46592 --- .../boot/logging/LoggingSystemProperties.java | 3 +++ .../additional-spring-configuration-metadata.json | 6 ++++++ .../logging/LoggingSystemPropertiesTests.java | 15 +++++++++++++++ .../modules/reference/pages/features/logging.adoc | 2 ++ .../logback/SampleLogbackApplicationTests.java | 6 ++++++ 5 files changed, 32 insertions(+) diff --git a/core/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperties.java b/core/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperties.java index 40390b80a93..9b4497a02bb 100644 --- a/core/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperties.java +++ b/core/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperties.java @@ -143,6 +143,9 @@ public class LoggingSystemProperties { if (logFile != null) { logFile.applyToSystemProperties(); } + if (!this.environment.getProperty("logging.console.enabled", Boolean.class, true)) { + setSystemProperty(LoggingSystemProperty.CONSOLE_THRESHOLD.getEnvironmentVariableName(), "OFF"); + } } /** diff --git a/core/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/core/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json index f01600c74d4..28e404f9351 100644 --- a/core/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/core/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -29,6 +29,12 @@ "description": "Location of the logging configuration file. For instance, `classpath:logback.xml` for Logback.", "sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener" }, + { + "name": "logging.console.enabled", + "type": "java.lang.Boolean", + "description": "Whether to enable console based logging", + "sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener" + }, { "name": "logging.exception-conversion-word", "type": "java.lang.String", diff --git a/core/spring-boot/src/test/java/org/springframework/boot/logging/LoggingSystemPropertiesTests.java b/core/spring-boot/src/test/java/org/springframework/boot/logging/LoggingSystemPropertiesTests.java index 6aaf3ee90f6..7475f31db8d 100644 --- a/core/spring-boot/src/test/java/org/springframework/boot/logging/LoggingSystemPropertiesTests.java +++ b/core/spring-boot/src/test/java/org/springframework/boot/logging/LoggingSystemPropertiesTests.java @@ -232,6 +232,21 @@ class LoggingSystemPropertiesTests { .isEqualTo("ecs"); } + @Test + void shouldSetConsoleLevelThreshholdToOffWhenConsoleLoggingDisabled() { + new LoggingSystemProperties(new MockEnvironment().withProperty("logging.console.enabled", "false")).apply(null); + assertThat(System.getProperty(LoggingSystemProperty.CONSOLE_THRESHOLD.getEnvironmentVariableName())) + .isEqualTo("OFF"); + } + + @Test + void shouldNotChangeConsoleLevelThreshholdWhenConsoleLoggingEnabled() { + new LoggingSystemProperties(new MockEnvironment().withProperty("logging.console.enabled", "true") + .withProperty("logging.threshold.console", "TRACE")).apply(null); + assertThat(System.getProperty(LoggingSystemProperty.CONSOLE_THRESHOLD.getEnvironmentVariableName())) + .isEqualTo("TRACE"); + } + private Environment environment(String key, Object value) { StandardEnvironment environment = new StandardEnvironment(); environment.getPropertySources().addLast(new MapPropertySource("test", Collections.singletonMap(key, value))); diff --git a/documentation/spring-boot-docs/src/docs/antora/modules/reference/pages/features/logging.adoc b/documentation/spring-boot-docs/src/docs/antora/modules/reference/pages/features/logging.adoc index 5b0c0765c66..453b7938af2 100644 --- a/documentation/spring-boot-docs/src/docs/antora/modules/reference/pages/features/logging.adoc +++ b/documentation/spring-boot-docs/src/docs/antora/modules/reference/pages/features/logging.adoc @@ -71,6 +71,8 @@ Enabling the debug mode does _not_ configure your application to log all message Alternatively, you can enable a "`trace`" mode by starting your application with a `--trace` flag (or `trace=true` in your `application.properties`). Doing so enables trace logging for a selection of core loggers (embedded container, Hibernate schema generation, and the whole Spring portfolio). +If you wan to disable console based logging, you can set the configprop:logging.console.enabled[] property to `false`. + [[features.logging.console-output.color-coded]] diff --git a/smoke-test/spring-boot-smoke-test-logback/src/test/java/smoketest/logback/SampleLogbackApplicationTests.java b/smoke-test/spring-boot-smoke-test-logback/src/test/java/smoketest/logback/SampleLogbackApplicationTests.java index 8e65bba78d6..de784f46356 100644 --- a/smoke-test/spring-boot-smoke-test-logback/src/test/java/smoketest/logback/SampleLogbackApplicationTests.java +++ b/smoke-test/spring-boot-smoke-test-logback/src/test/java/smoketest/logback/SampleLogbackApplicationTests.java @@ -39,4 +39,10 @@ class SampleLogbackApplicationTests { assertThat(output).contains("Sample Debug Message").contains("Sample Trace Message"); } + @Test + void testDisableConsoleLogging(CapturedOutput output) { + SampleLogbackApplication.main(new String[] { "--logging.console.enabled=false" }); + assertThat(output).doesNotContain("---"); + } + }