Automatically disable banner when using structured logging
Closes gh-41659
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1647,7 +1647,7 @@ class SpringApplicationTests {
|
||||
}
|
||||
|
||||
Banner.Mode getBannerMode() {
|
||||
return this.properties.getBannerMode();
|
||||
return this.properties.getBannerMode(new MockEnvironment());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
spring.main.banner-mode=off
|
||||
logging.structured.format.console=ecs
|
||||
#---
|
||||
spring.config.activate.on-profile=custom
|
||||
|
||||
@@ -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,4 +1,3 @@
|
||||
spring.main.banner-mode=off
|
||||
logging.structured.format.console=ecs
|
||||
#---
|
||||
spring.config.activate.on-profile=custom
|
||||
|
||||
@@ -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]);
|
||||
|
||||
Reference in New Issue
Block a user