Browse Source

Automatically disable banner when using structured logging

Closes gh-41659
pull/41975/head
Moritz Halbritter 1 year ago
parent
commit
25c76957e5
  1. 13
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ApplicationProperties.java
  2. 4
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java
  3. 8
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperty.java
  4. 3
      spring-boot-project/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json
  5. 47
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/ApplicationPropertiesTests.java
  6. 2
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java
  7. 1
      spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-structure-logging-log4j2/src/main/resources/application.properties
  8. 6
      spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-structure-logging-log4j2/src/test/java/smoketest/structuredlogging/log4j2/SampleLog4j2StructuredLoggingApplicationTests.java
  9. 1
      spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-structure-logging/src/main/resources/application.properties
  10. 6
      spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-structure-logging/src/test/java/smoketest/structuredlogging/SampleStructuredLoggingApplicationTests.java

13
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ApplicationProperties.java

@ -20,6 +20,8 @@ import java.util.LinkedHashSet; @@ -20,6 +20,8 @@ import java.util.LinkedHashSet;
import java.util.Set;
import org.springframework.boot.Banner.Mode;
import org.springframework.boot.logging.LoggingSystemProperty;
import org.springframework.core.env.Environment;
/**
* Spring application properties.
@ -43,7 +45,7 @@ class ApplicationProperties { @@ -43,7 +45,7 @@ class ApplicationProperties {
/**
* Mode used to display the banner when the application runs.
*/
private Banner.Mode bannerMode = Banner.Mode.CONSOLE;
private Banner.Mode bannerMode;
/**
* Whether to keep the application alive even if there are no more non-daemon threads.
@ -93,8 +95,13 @@ class ApplicationProperties { @@ -93,8 +95,13 @@ class ApplicationProperties {
this.allowCircularReferences = allowCircularReferences;
}
Mode getBannerMode() {
return this.bannerMode;
Mode getBannerMode(Environment environment) {
if (this.bannerMode != null) {
return this.bannerMode;
}
boolean structuredLoggingEnabled = environment
.containsProperty(LoggingSystemProperty.CONSOLE_STRUCTURED_FORMAT.getApplicationPropertyName());
return (structuredLoggingEnabled) ? Mode.OFF : Banner.Mode.CONSOLE;
}
void setBannerMode(Mode bannerMode) {

4
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java

@ -555,13 +555,13 @@ public class SpringApplication { @@ -555,13 +555,13 @@ public class SpringApplication {
}
private Banner printBanner(ConfigurableEnvironment environment) {
if (this.properties.getBannerMode() == Banner.Mode.OFF) {
if (this.properties.getBannerMode(environment) == Banner.Mode.OFF) {
return null;
}
ResourceLoader resourceLoader = (this.resourceLoader != null) ? this.resourceLoader
: new DefaultResourceLoader(null);
SpringApplicationBannerPrinter bannerPrinter = new SpringApplicationBannerPrinter(resourceLoader, this.banner);
if (this.properties.getBannerMode() == Mode.LOG) {
if (this.properties.getBannerMode(environment) == Mode.LOG) {
return bannerPrinter.print(environment, this.mainApplicationClass, logger);
}
return bannerPrinter.print(environment, this.mainApplicationClass, System.out);

8
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperty.java

@ -140,7 +140,13 @@ public enum LoggingSystemProperty { @@ -140,7 +140,13 @@ public enum LoggingSystemProperty {
return this.environmentVariableName;
}
String getApplicationPropertyName() {
/**
* Return the name of the application property name that can be used to set this
* property.
* @return the application property name
* @since 3.4.0
*/
public String getApplicationPropertyName() {
return this.applicationPropertyName;
}

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

@ -426,8 +426,7 @@ @@ -426,8 +426,7 @@
"name": "spring.main.banner-mode",
"type": "org.springframework.boot.Banner$Mode",
"sourceType": "org.springframework.boot.SpringApplication",
"description": "Mode used to display the banner when the application runs.",
"defaultValue": "console"
"description": "Mode used to display the banner when the application runs. Defaults to 'off' if structured logging is enabled or to 'console' otherwise"
},
{
"name": "spring.main.cloud-platform",

47
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/ApplicationPropertiesTests.java

@ -0,0 +1,47 @@ @@ -0,0 +1,47 @@
/*
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot;
import org.junit.jupiter.api.Test;
import org.springframework.boot.Banner.Mode;
import org.springframework.mock.env.MockEnvironment;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link ApplicationProperties}.
*
* @author Moritz Halbritter
*/
class ApplicationPropertiesTests {
@Test
void bannerModeShouldBeConsoleIfStructuredLoggingIsNotEnabled() {
ApplicationProperties properties = new ApplicationProperties();
assertThat(properties.getBannerMode(new MockEnvironment())).isEqualTo(Mode.CONSOLE);
}
@Test
void bannerModeShouldBeOffIfStructuredLoggingIsEnabled() {
ApplicationProperties properties = new ApplicationProperties();
MockEnvironment environment = new MockEnvironment();
environment.setProperty("logging.structured.format.console", "ecs");
assertThat(properties.getBannerMode(environment)).isEqualTo(Mode.OFF);
}
}

2
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java

@ -1647,7 +1647,7 @@ class SpringApplicationTests { @@ -1647,7 +1647,7 @@ class SpringApplicationTests {
}
Banner.Mode getBannerMode() {
return this.properties.getBannerMode();
return this.properties.getBannerMode(new MockEnvironment());
}
}

1
spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-structure-logging-log4j2/src/main/resources/application.properties

@ -1,4 +1,3 @@ @@ -1,4 +1,3 @@
spring.main.banner-mode=off
logging.structured.format.console=ecs
#---
spring.config.activate.on-profile=custom

6
spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-structure-logging-log4j2/src/test/java/smoketest/structuredlogging/log4j2/SampleLog4j2StructuredLoggingApplicationTests.java

@ -43,6 +43,12 @@ class SampleLog4j2StructuredLoggingApplicationTests { @@ -43,6 +43,12 @@ class SampleLog4j2StructuredLoggingApplicationTests {
}
}
@Test
void shouldNotLogBanner(CapturedOutput output) {
SampleLog4j2StructuredLoggingApplication.main(new String[0]);
assertThat(output).doesNotContain(" :: Spring Boot :: ");
}
@Test
void json(CapturedOutput output) {
SampleLog4j2StructuredLoggingApplication.main(new String[0]);

1
spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-structure-logging/src/main/resources/application.properties

@ -1,4 +1,3 @@ @@ -1,4 +1,3 @@
spring.main.banner-mode=off
logging.structured.format.console=ecs
#---
spring.config.activate.on-profile=custom

6
spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-structure-logging/src/test/java/smoketest/structuredlogging/SampleStructuredLoggingApplicationTests.java

@ -43,6 +43,12 @@ class SampleStructuredLoggingApplicationTests { @@ -43,6 +43,12 @@ class SampleStructuredLoggingApplicationTests {
}
}
@Test
void shouldNotLogBanner(CapturedOutput output) {
SampleStructuredLoggingApplication.main(new String[0]);
assertThat(output).doesNotContain(" :: Spring Boot :: ");
}
@Test
void json(CapturedOutput output) {
SampleStructuredLoggingApplication.main(new String[0]);

Loading…
Cancel
Save