Skip to content

Commit c52f6f0

Browse files
committed
Merge branch '2.5.x' into 2.6.x
Closes gh-29915
2 parents 95d67a9 + ca23c68 commit c52f6f0

File tree

3 files changed

+42
-13
lines changed

3 files changed

+42
-13
lines changed

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Map;
2929
import java.util.Properties;
3030
import java.util.Set;
31+
import java.util.stream.Collectors;
3132

3233
import org.apache.commons.logging.Log;
3334
import org.apache.commons.logging.LogFactory;
@@ -631,19 +632,26 @@ protected void logStartupInfo(boolean isRoot) {
631632
protected void logStartupProfileInfo(ConfigurableApplicationContext context) {
632633
Log log = getApplicationLog();
633634
if (log.isInfoEnabled()) {
634-
String[] activeProfiles = context.getEnvironment().getActiveProfiles();
635+
List<String> activeProfiles = quoteProfiles(context.getEnvironment().getActiveProfiles());
635636
if (ObjectUtils.isEmpty(activeProfiles)) {
636-
String[] defaultProfiles = context.getEnvironment().getDefaultProfiles();
637-
log.info("No active profile set, falling back to default profiles: "
638-
+ StringUtils.arrayToCommaDelimitedString(defaultProfiles));
637+
List<String> defaultProfiles = quoteProfiles(context.getEnvironment().getDefaultProfiles());
638+
String message = String.format("%s default %s: ", defaultProfiles.size(),
639+
(defaultProfiles.size() <= 1) ? "profile" : "profiles");
640+
log.info("No active profile set, falling back to " + message
641+
+ StringUtils.collectionToDelimitedString(defaultProfiles, ", "));
639642
}
640643
else {
641-
log.info("The following profiles are active: "
642-
+ StringUtils.arrayToCommaDelimitedString(activeProfiles));
644+
String message = (activeProfiles.size() == 1) ? "1 profile is active: "
645+
: activeProfiles.size() + " profiles are active: ";
646+
log.info("The following " + message + StringUtils.collectionToDelimitedString(activeProfiles, ", "));
643647
}
644648
}
645649
}
646650

651+
private List<String> quoteProfiles(String[] profiles) {
652+
return Arrays.stream(profiles).map((profile) -> "\"" + profile + "\"").collect(Collectors.toList());
653+
}
654+
647655
/**
648656
* Returns the {@link Log} for the application. By default will be deduced.
649657
* @return the application log

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,19 +254,40 @@ void imageBannerLoads(CapturedOutput output) {
254254
}
255255

256256
@Test
257-
void logsNoActiveProfiles(CapturedOutput output) {
257+
void logsActiveProfilesWithoutProfileAndSingleDefault(CapturedOutput output) {
258258
SpringApplication application = new SpringApplication(ExampleConfig.class);
259259
application.setWebApplicationType(WebApplicationType.NONE);
260260
this.context = application.run();
261-
assertThat(output).contains("No active profile set, falling back to default profiles: default");
261+
assertThat(output).contains("No active profile set, falling back to 1 default profile: \"default\"");
262262
}
263263

264264
@Test
265-
void logsActiveProfiles(CapturedOutput output) {
265+
void logsActiveProfilesWithoutProfileAndMultipleDefaults(CapturedOutput output) {
266+
MockEnvironment environment = new MockEnvironment();
267+
environment.setDefaultProfiles("p0,p1", "default");
268+
SpringApplication application = new SpringApplication(ExampleConfig.class);
269+
application.setWebApplicationType(WebApplicationType.NONE);
270+
application.setEnvironment(environment);
271+
this.context = application.run();
272+
assertThat(output)
273+
.contains("No active profile set, falling back to 2 default profiles: \"p0,p1\", \"default\"");
274+
}
275+
276+
@Test
277+
void logsActiveProfilesWithSingleProfile(CapturedOutput output) {
266278
SpringApplication application = new SpringApplication(ExampleConfig.class);
267279
application.setWebApplicationType(WebApplicationType.NONE);
268280
this.context = application.run("--spring.profiles.active=myprofiles");
269-
assertThat(output).contains("The following profiles are active: myprofile");
281+
assertThat(output).contains("The following 1 profile is active: \"myprofiles\"");
282+
}
283+
284+
@Test
285+
void logsActiveProfilesWithMultipleProfiles(CapturedOutput output) {
286+
SpringApplication application = new SpringApplication(ExampleConfig.class);
287+
application.setWebApplicationType(WebApplicationType.NONE);
288+
application.setAdditionalProfiles("p1,p2", "p3");
289+
application.run();
290+
assertThat(output).contains("The following 2 profiles are active: \"p1,p2\", \"p3\"");
270291
}
271292

272293
@Test

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -815,8 +815,8 @@ void activateProfileFromProfileSpecificProperties(CapturedOutput output) {
815815
assertThat(environment).has(matchingProfile("morespecific"));
816816
assertThat(environment).has(matchingProfile("yetmorespecific"));
817817
assertThat(environment).doesNotHave(matchingProfile("missing"));
818-
assertThat(output)
819-
.contains("The following profiles are active: includeprofile,specific,morespecific,yetmorespecific");
818+
assertThat(output).contains(
819+
"The following 4 profiles are active: \"includeprofile\", \"specific\", \"morespecific\", \"yetmorespecific\"");
820820
}
821821

822822
@Test

0 commit comments

Comments
 (0)