Skip to content

Commit 25c7695

Browse files
committed
Automatically disable banner when using structured logging
Closes gh-41659
1 parent a70ff35 commit 25c7695

File tree

10 files changed

+80
-11
lines changed

10 files changed

+80
-11
lines changed

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

+10-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.util.Set;
2121

2222
import org.springframework.boot.Banner.Mode;
23+
import org.springframework.boot.logging.LoggingSystemProperty;
24+
import org.springframework.core.env.Environment;
2325

2426
/**
2527
* Spring application properties.
@@ -43,7 +45,7 @@ class ApplicationProperties {
4345
/**
4446
* Mode used to display the banner when the application runs.
4547
*/
46-
private Banner.Mode bannerMode = Banner.Mode.CONSOLE;
48+
private Banner.Mode bannerMode;
4749

4850
/**
4951
* Whether to keep the application alive even if there are no more non-daemon threads.
@@ -93,8 +95,13 @@ void setAllowCircularReferences(boolean allowCircularReferences) {
9395
this.allowCircularReferences = allowCircularReferences;
9496
}
9597

96-
Mode getBannerMode() {
97-
return this.bannerMode;
98+
Mode getBannerMode(Environment environment) {
99+
if (this.bannerMode != null) {
100+
return this.bannerMode;
101+
}
102+
boolean structuredLoggingEnabled = environment
103+
.containsProperty(LoggingSystemProperty.CONSOLE_STRUCTURED_FORMAT.getApplicationPropertyName());
104+
return (structuredLoggingEnabled) ? Mode.OFF : Banner.Mode.CONSOLE;
98105
}
99106

100107
void setBannerMode(Mode bannerMode) {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -555,13 +555,13 @@ protected void bindToSpringApplication(ConfigurableEnvironment environment) {
555555
}
556556

557557
private Banner printBanner(ConfigurableEnvironment environment) {
558-
if (this.properties.getBannerMode() == Banner.Mode.OFF) {
558+
if (this.properties.getBannerMode(environment) == Banner.Mode.OFF) {
559559
return null;
560560
}
561561
ResourceLoader resourceLoader = (this.resourceLoader != null) ? this.resourceLoader
562562
: new DefaultResourceLoader(null);
563563
SpringApplicationBannerPrinter bannerPrinter = new SpringApplicationBannerPrinter(resourceLoader, this.banner);
564-
if (this.properties.getBannerMode() == Mode.LOG) {
564+
if (this.properties.getBannerMode(environment) == Mode.LOG) {
565565
return bannerPrinter.print(environment, this.mainApplicationClass, logger);
566566
}
567567
return bannerPrinter.print(environment, this.mainApplicationClass, System.out);

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,13 @@ public String getEnvironmentVariableName() {
140140
return this.environmentVariableName;
141141
}
142142

143-
String getApplicationPropertyName() {
143+
/**
144+
* Return the name of the application property name that can be used to set this
145+
* property.
146+
* @return the application property name
147+
* @since 3.4.0
148+
*/
149+
public String getApplicationPropertyName() {
144150
return this.applicationPropertyName;
145151
}
146152

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,7 @@
426426
"name": "spring.main.banner-mode",
427427
"type": "org.springframework.boot.Banner$Mode",
428428
"sourceType": "org.springframework.boot.SpringApplication",
429-
"description": "Mode used to display the banner when the application runs.",
430-
"defaultValue": "console"
429+
"description": "Mode used to display the banner when the application runs. Defaults to 'off' if structured logging is enabled or to 'console' otherwise"
431430
},
432431
{
433432
"name": "spring.main.cloud-platform",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.boot.Banner.Mode;
22+
import org.springframework.mock.env.MockEnvironment;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
/**
27+
* Tests for {@link ApplicationProperties}.
28+
*
29+
* @author Moritz Halbritter
30+
*/
31+
class ApplicationPropertiesTests {
32+
33+
@Test
34+
void bannerModeShouldBeConsoleIfStructuredLoggingIsNotEnabled() {
35+
ApplicationProperties properties = new ApplicationProperties();
36+
assertThat(properties.getBannerMode(new MockEnvironment())).isEqualTo(Mode.CONSOLE);
37+
}
38+
39+
@Test
40+
void bannerModeShouldBeOffIfStructuredLoggingIsEnabled() {
41+
ApplicationProperties properties = new ApplicationProperties();
42+
MockEnvironment environment = new MockEnvironment();
43+
environment.setProperty("logging.structured.format.console", "ecs");
44+
assertThat(properties.getBannerMode(environment)).isEqualTo(Mode.OFF);
45+
}
46+
47+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1647,7 +1647,7 @@ BeanDefinitionLoader getLoader() {
16471647
}
16481648

16491649
Banner.Mode getBannerMode() {
1650-
return this.properties.getBannerMode();
1650+
return this.properties.getBannerMode(new MockEnvironment());
16511651
}
16521652

16531653
}

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

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
spring.main.banner-mode=off
21
logging.structured.format.console=ecs
32
#---
43
spring.config.activate.on-profile=custom

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

+6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ void reset() {
4343
}
4444
}
4545

46+
@Test
47+
void shouldNotLogBanner(CapturedOutput output) {
48+
SampleLog4j2StructuredLoggingApplication.main(new String[0]);
49+
assertThat(output).doesNotContain(" :: Spring Boot :: ");
50+
}
51+
4652
@Test
4753
void json(CapturedOutput output) {
4854
SampleLog4j2StructuredLoggingApplication.main(new String[0]);

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

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
spring.main.banner-mode=off
21
logging.structured.format.console=ecs
32
#---
43
spring.config.activate.on-profile=custom

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

+6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ void reset() {
4343
}
4444
}
4545

46+
@Test
47+
void shouldNotLogBanner(CapturedOutput output) {
48+
SampleStructuredLoggingApplication.main(new String[0]);
49+
assertThat(output).doesNotContain(" :: Spring Boot :: ");
50+
}
51+
4652
@Test
4753
void json(CapturedOutput output) {
4854
SampleStructuredLoggingApplication.main(new String[0]);

0 commit comments

Comments
 (0)