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 2324e2ea3f..7f1910c611 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 @@ -28,6 +28,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; @@ -619,19 +620,26 @@ public class SpringApplication { protected void logStartupProfileInfo(ConfigurableApplicationContext context) { Log log = getApplicationLog(); if (log.isInfoEnabled()) { - String[] activeProfiles = context.getEnvironment().getActiveProfiles(); + List activeProfiles = quoteProfiles(context.getEnvironment().getActiveProfiles()); if (ObjectUtils.isEmpty(activeProfiles)) { - String[] defaultProfiles = context.getEnvironment().getDefaultProfiles(); - log.info("No active profile set, falling back to default profiles: " - + StringUtils.arrayToCommaDelimitedString(defaultProfiles)); + List 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 profiles 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 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 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 3c266e2447..0d74c3ab6c 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 @@ -231,19 +231,40 @@ 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 default profiles: 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); + 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 logsActiveProfilesWithSingleProfile(CapturedOutput output) { SpringApplication application = new SpringApplication(ExampleConfig.class); application.setWebApplicationType(WebApplicationType.NONE); this.context = application.run("--spring.profiles.active=myprofiles"); - assertThat(output).contains("The following profiles are active: myprofile"); + assertThat(output).contains("The following 1 profile is active: \"myprofiles\""); + } + + @Test + 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 profiles are active: \"p1,p2\", \"p3\""); } @Test