Browse Source

Polish "Clarify log message with a profile containing a comma"

See gh-29896
pull/29932/head
Stephane Nicoll 4 years ago
parent
commit
92cd51e6b0
  1. 26
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java
  2. 36
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java
  3. 6
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java

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

@ -27,6 +27,7 @@ import java.util.List; @@ -27,6 +27,7 @@ import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -666,25 +667,26 @@ public class SpringApplication { @@ -666,25 +667,26 @@ public class SpringApplication {
protected void logStartupProfileInfo(ConfigurableApplicationContext context) {
Log log = getApplicationLog();
if (log.isInfoEnabled()) {
String[] activeProfiles = context.getEnvironment().getActiveProfiles();
for (int i = 0; i < activeProfiles.length; i++) {
activeProfiles[i] = "\"" + activeProfiles[i] + "\"";
}
List<String> activeProfiles = quoteProfiles(context.getEnvironment().getActiveProfiles());
if (ObjectUtils.isEmpty(activeProfiles)) {
String[] defaultProfiles = context.getEnvironment().getDefaultProfiles();
for (int i = 0; i < defaultProfiles.length; i++) {
defaultProfiles[i] = "\"" + defaultProfiles[i] + "\"";
}
log.info("No active profile set, falling back to " + defaultProfiles.length + " default profile(s): "
+ StringUtils.arrayToCommaDelimitedString(defaultProfiles));
List<String> defaultProfiles = quoteProfiles(context.getEnvironment().getDefaultProfiles());
String message = String.format("%s default %s: ", defaultProfiles.size(),
(defaultProfiles.size() <= 1) ? "profile" : "profiles");
log.info("No active profile set, falling back to " + message
+ StringUtils.collectionToDelimitedString(defaultProfiles, ", "));
}
else {
log.info("The following " + activeProfiles.length + " profile(s) are active: "
+ StringUtils.arrayToCommaDelimitedString(activeProfiles));
String message = (activeProfiles.size() == 1) ? "1 profile is active: "
: activeProfiles.size() + " profiles are active: ";
log.info("The following " + message + StringUtils.collectionToDelimitedString(activeProfiles, ", "));
}
}
}
private List<String> quoteProfiles(String[] profiles) {
return Arrays.stream(profiles).map((profile) -> "\"" + profile + "\"").collect(Collectors.toList());
}
/**
* Returns the {@link Log} for the application. By default will be deduced.
* @return the application log

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

@ -249,37 +249,49 @@ class SpringApplicationTests { @@ -249,37 +249,49 @@ class SpringApplicationTests {
}
@Test
void logsNoActiveProfiles(CapturedOutput output) {
void logsActiveProfilesWithoutProfileAndSingleDefault(CapturedOutput output) {
SpringApplication application = new SpringApplication(ExampleConfig.class);
application.setWebApplicationType(WebApplicationType.NONE);
this.context = application.run();
assertThat(output).contains("No active profile set, falling back to 1 default profile(s): \"default\"");
assertThat(output).contains("No active profile set, falling back to 1 default profile: \"default\"");
}
@Test
void logsActiveProfiles(CapturedOutput output) {
void logsActiveProfilesWithoutProfileAndMultipleDefaults(CapturedOutput output) {
MockEnvironment environment = new MockEnvironment();
environment.setDefaultProfiles("p0,p1", "default");
SpringApplication application = new SpringApplication(ExampleConfig.class);
application.setWebApplicationType(WebApplicationType.NONE);
this.context = application.run("--spring.profiles.active=myprofiles");
assertThat(output).contains("The following 1 profile(s) are active: \"myprofiles\"");
application.setEnvironment(environment);
this.context = application.run();
assertThat(output)
.contains("No active profile set, falling back to 2 default profiles: \"p0,p1\", \"default\"");
}
@Test
void enableBannerInLogViaProperty(CapturedOutput output) {
SpringApplication application = spy(new SpringApplication(ExampleConfig.class));
void logsActiveProfilesWithSingleProfile(CapturedOutput output) {
SpringApplication application = new SpringApplication(ExampleConfig.class);
application.setWebApplicationType(WebApplicationType.NONE);
this.context = application.run("--spring.main.banner-mode=log");
then(application).should(atLeastOnce()).setBannerMode(Banner.Mode.LOG);
assertThat(output).contains("o.s.b.SpringApplication");
this.context = application.run("--spring.profiles.active=myprofiles");
assertThat(output).contains("The following 1 profile is active: \"myprofiles\"");
}
@Test
void logsMultipleActiveProfilesWithComma(CapturedOutput output) {
void logsActiveProfilesWithMultipleProfiles(CapturedOutput output) {
SpringApplication application = new SpringApplication(ExampleConfig.class);
application.setWebApplicationType(WebApplicationType.NONE);
application.setAdditionalProfiles("p1,p2", "p3");
application.run();
assertThat(output).contains("The following 2 profile(s) are active: \"p1,p2\",\"p3\"");
assertThat(output).contains("The following 2 profiles are active: \"p1,p2\", \"p3\"");
}
@Test
void enableBannerInLogViaProperty(CapturedOutput output) {
SpringApplication application = spy(new SpringApplication(ExampleConfig.class));
application.setWebApplicationType(WebApplicationType.NONE);
this.context = application.run("--spring.main.banner-mode=log");
then(application).should(atLeastOnce()).setBannerMode(Banner.Mode.LOG);
assertThat(output).contains("o.s.b.SpringApplication");
}
@Test

6
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2022 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.
@ -815,8 +815,8 @@ class ConfigFileApplicationListenerTests { @@ -815,8 +815,8 @@ class ConfigFileApplicationListenerTests {
assertThat(environment).has(matchingProfile("morespecific"));
assertThat(environment).has(matchingProfile("yetmorespecific"));
assertThat(environment).doesNotHave(matchingProfile("missing"));
assertThat(output)
.contains("The following profiles are active: includeprofile,specific,morespecific,yetmorespecific");
assertThat(output).contains(
"The following 4 profiles are active: \"includeprofile\", \"specific\", \"morespecific\", \"yetmorespecific\"");
}
@Test

Loading…
Cancel
Save