Browse Source

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
pull/46935/head
Phillip Webb 4 months ago
parent
commit
ac2e6972d7
  1. 3
      core/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperties.java
  2. 6
      core/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json
  3. 15
      core/spring-boot/src/test/java/org/springframework/boot/logging/LoggingSystemPropertiesTests.java
  4. 2
      documentation/spring-boot-docs/src/docs/antora/modules/reference/pages/features/logging.adoc
  5. 6
      smoke-test/spring-boot-smoke-test-logback/src/test/java/smoketest/logback/SampleLogbackApplicationTests.java

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

@ -143,6 +143,9 @@ public class LoggingSystemProperties { @@ -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");
}
}
/**

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

@ -29,6 +29,12 @@ @@ -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",

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

@ -232,6 +232,21 @@ class LoggingSystemPropertiesTests { @@ -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)));

2
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 @@ -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]]

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

@ -39,4 +39,10 @@ class SampleLogbackApplicationTests { @@ -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("---");
}
}

Loading…
Cancel
Save