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
This commit is contained in:
Phillip Webb
2025-08-20 19:17:30 -07:00
parent 7140be8751
commit ac2e6972d7
5 changed files with 32 additions and 0 deletions
@@ -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");
}
}
/**
@@ -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",
@@ -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)));
@@ -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]]
@@ -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("---");
}
}