diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ApplicationProperties.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ApplicationProperties.java index d51696bc2fe..994e9381e0c 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ApplicationProperties.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ApplicationProperties.java @@ -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 { /** * 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 { 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) { diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java index bce52cbc427..fed80707e5a 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -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); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperty.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperty.java index d57d32b4034..d651d568452 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperty.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperty.java @@ -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; } diff --git a/spring-boot-project/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-boot-project/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 50eed03b92f..4921f63f3f1 100644 --- a/spring-boot-project/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-boot-project/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -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", diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/ApplicationPropertiesTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/ApplicationPropertiesTests.java new file mode 100644 index 00000000000..ead83d7c38b --- /dev/null +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/ApplicationPropertiesTests.java @@ -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); + } + +} diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java index 1ac19f48010..353553d14b8 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java @@ -1647,7 +1647,7 @@ class SpringApplicationTests { } Banner.Mode getBannerMode() { - return this.properties.getBannerMode(); + return this.properties.getBannerMode(new MockEnvironment()); } } diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-structure-logging-log4j2/src/main/resources/application.properties b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-structure-logging-log4j2/src/main/resources/application.properties index f0ec153c0c8..d943aa294c9 100644 --- a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-structure-logging-log4j2/src/main/resources/application.properties +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-structure-logging-log4j2/src/main/resources/application.properties @@ -1,4 +1,3 @@ -spring.main.banner-mode=off logging.structured.format.console=ecs #--- spring.config.activate.on-profile=custom diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-structure-logging-log4j2/src/test/java/smoketest/structuredlogging/log4j2/SampleLog4j2StructuredLoggingApplicationTests.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-structure-logging-log4j2/src/test/java/smoketest/structuredlogging/log4j2/SampleLog4j2StructuredLoggingApplicationTests.java index d8a99e4e9ed..1c3bd6e8b6d 100644 --- a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-structure-logging-log4j2/src/test/java/smoketest/structuredlogging/log4j2/SampleLog4j2StructuredLoggingApplicationTests.java +++ b/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 { } } + @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]); diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-structure-logging/src/main/resources/application.properties b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-structure-logging/src/main/resources/application.properties index b04549b907e..e5911010313 100644 --- a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-structure-logging/src/main/resources/application.properties +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-structure-logging/src/main/resources/application.properties @@ -1,4 +1,3 @@ -spring.main.banner-mode=off logging.structured.format.console=ecs #--- spring.config.activate.on-profile=custom diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-structure-logging/src/test/java/smoketest/structuredlogging/SampleStructuredLoggingApplicationTests.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-structure-logging/src/test/java/smoketest/structuredlogging/SampleStructuredLoggingApplicationTests.java index 0d77b17625a..4db380c25bb 100644 --- a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-structure-logging/src/test/java/smoketest/structuredlogging/SampleStructuredLoggingApplicationTests.java +++ b/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 { } } + @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]);