Skip to content

Commit ac2e697

Browse files
committed
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
1 parent 7140be8 commit ac2e697

File tree

5 files changed

+32
-0
lines changed

5 files changed

+32
-0
lines changed

core/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperties.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ protected void apply(@Nullable LogFile logFile, PropertyResolver resolver) {
143143
if (logFile != null) {
144144
logFile.applyToSystemProperties();
145145
}
146+
if (!this.environment.getProperty("logging.console.enabled", Boolean.class, true)) {
147+
setSystemProperty(LoggingSystemProperty.CONSOLE_THRESHOLD.getEnvironmentVariableName(), "OFF");
148+
}
146149
}
147150

148151
/**

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929
"description": "Location of the logging configuration file. For instance, `classpath:logback.xml` for Logback.",
3030
"sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener"
3131
},
32+
{
33+
"name": "logging.console.enabled",
34+
"type": "java.lang.Boolean",
35+
"description": "Whether to enable console based logging",
36+
"sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener"
37+
},
3238
{
3339
"name": "logging.exception-conversion-word",
3440
"type": "java.lang.String",

core/spring-boot/src/test/java/org/springframework/boot/logging/LoggingSystemPropertiesTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,21 @@ void shouldSetConsoleStructuredLogging() {
232232
.isEqualTo("ecs");
233233
}
234234

235+
@Test
236+
void shouldSetConsoleLevelThreshholdToOffWhenConsoleLoggingDisabled() {
237+
new LoggingSystemProperties(new MockEnvironment().withProperty("logging.console.enabled", "false")).apply(null);
238+
assertThat(System.getProperty(LoggingSystemProperty.CONSOLE_THRESHOLD.getEnvironmentVariableName()))
239+
.isEqualTo("OFF");
240+
}
241+
242+
@Test
243+
void shouldNotChangeConsoleLevelThreshholdWhenConsoleLoggingEnabled() {
244+
new LoggingSystemProperties(new MockEnvironment().withProperty("logging.console.enabled", "true")
245+
.withProperty("logging.threshold.console", "TRACE")).apply(null);
246+
assertThat(System.getProperty(LoggingSystemProperty.CONSOLE_THRESHOLD.getEnvironmentVariableName()))
247+
.isEqualTo("TRACE");
248+
}
249+
235250
private Environment environment(String key, Object value) {
236251
StandardEnvironment environment = new StandardEnvironment();
237252
environment.getPropertySources().addLast(new MapPropertySource("test", Collections.singletonMap(key, value)));

documentation/spring-boot-docs/src/docs/antora/modules/reference/pages/features/logging.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ Enabling the debug mode does _not_ configure your application to log all message
7171
Alternatively, you can enable a "`trace`" mode by starting your application with a `--trace` flag (or `trace=true` in your `application.properties`).
7272
Doing so enables trace logging for a selection of core loggers (embedded container, Hibernate schema generation, and the whole Spring portfolio).
7373

74+
If you wan to disable console based logging, you can set the configprop:logging.console.enabled[] property to `false`.
75+
7476

7577

7678
[[features.logging.console-output.color-coded]]

smoke-test/spring-boot-smoke-test-logback/src/test/java/smoketest/logback/SampleLogbackApplicationTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,10 @@ void testProfile(CapturedOutput output) {
3939
assertThat(output).contains("Sample Debug Message").contains("Sample Trace Message");
4040
}
4141

42+
@Test
43+
void testDisableConsoleLogging(CapturedOutput output) {
44+
SampleLogbackApplication.main(new String[] { "--logging.console.enabled=false" });
45+
assertThat(output).doesNotContain("---");
46+
}
47+
4248
}

0 commit comments

Comments
 (0)