Skip to content

Commit ca23c68

Browse files
committed
Merge pull request #29896 from gcoppex
* pr/29896: Polish "Clarify log message with a profile containing a comma" Clarify log message with a profile containing a comma Closes gh-29896
2 parents 5baa71f + 92cd51e commit ca23c68

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
@@ -27,6 +27,7 @@
2727
import java.util.Map;
2828
import java.util.Properties;
2929
import java.util.Set;
30+
import java.util.stream.Collectors;
3031

3132
import org.apache.commons.logging.Log;
3233
import org.apache.commons.logging.LogFactory;
@@ -666,19 +667,26 @@ protected void logStartupInfo(boolean isRoot) {
666667
protected void logStartupProfileInfo(ConfigurableApplicationContext context) {
667668
Log log = getApplicationLog();
668669
if (log.isInfoEnabled()) {
669-
String[] activeProfiles = context.getEnvironment().getActiveProfiles();
670+
List<String> activeProfiles = quoteProfiles(context.getEnvironment().getActiveProfiles());
670671
if (ObjectUtils.isEmpty(activeProfiles)) {
671-
String[] defaultProfiles = context.getEnvironment().getDefaultProfiles();
672-
log.info("No active profile set, falling back to default profiles: "
673-
+ StringUtils.arrayToCommaDelimitedString(defaultProfiles));
672+
List<String> defaultProfiles = quoteProfiles(context.getEnvironment().getDefaultProfiles());
673+
String message = String.format("%s default %s: ", defaultProfiles.size(),
674+
(defaultProfiles.size() <= 1) ? "profile" : "profiles");
675+
log.info("No active profile set, falling back to " + message
676+
+ StringUtils.collectionToDelimitedString(defaultProfiles, ", "));
674677
}
675678
else {
676-
log.info("The following profiles are active: "
677-
+ StringUtils.arrayToCommaDelimitedString(activeProfiles));
679+
String message = (activeProfiles.size() == 1) ? "1 profile is active: "
680+
: activeProfiles.size() + " profiles are active: ";
681+
log.info("The following " + message + StringUtils.collectionToDelimitedString(activeProfiles, ", "));
678682
}
679683
}
680684
}
681685

686+
private List<String> quoteProfiles(String[] profiles) {
687+
return Arrays.stream(profiles).map((profile) -> "\"" + profile + "\"").collect(Collectors.toList());
688+
}
689+
682690
/**
683691
* Returns the {@link Log} for the application. By default will be deduced.
684692
* @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
@@ -249,19 +249,40 @@ void imageBannerLoads(CapturedOutput output) {
249249
}
250250

251251
@Test
252-
void logsNoActiveProfiles(CapturedOutput output) {
252+
void logsActiveProfilesWithoutProfileAndSingleDefault(CapturedOutput output) {
253253
SpringApplication application = new SpringApplication(ExampleConfig.class);
254254
application.setWebApplicationType(WebApplicationType.NONE);
255255
this.context = application.run();
256-
assertThat(output).contains("No active profile set, falling back to default profiles: default");
256+
assertThat(output).contains("No active profile set, falling back to 1 default profile: \"default\"");
257257
}
258258

259259
@Test
260-
void logsActiveProfiles(CapturedOutput output) {
260+
void logsActiveProfilesWithoutProfileAndMultipleDefaults(CapturedOutput output) {
261+
MockEnvironment environment = new MockEnvironment();
262+
environment.setDefaultProfiles("p0,p1", "default");
263+
SpringApplication application = new SpringApplication(ExampleConfig.class);
264+
application.setWebApplicationType(WebApplicationType.NONE);
265+
application.setEnvironment(environment);
266+
this.context = application.run();
267+
assertThat(output)
268+
.contains("No active profile set, falling back to 2 default profiles: \"p0,p1\", \"default\"");
269+
}
270+
271+
@Test
272+
void logsActiveProfilesWithSingleProfile(CapturedOutput output) {
261273
SpringApplication application = new SpringApplication(ExampleConfig.class);
262274
application.setWebApplicationType(WebApplicationType.NONE);
263275
this.context = application.run("--spring.profiles.active=myprofiles");
264-
assertThat(output).contains("The following profiles are active: myprofile");
276+
assertThat(output).contains("The following 1 profile is active: \"myprofiles\"");
277+
}
278+
279+
@Test
280+
void logsActiveProfilesWithMultipleProfiles(CapturedOutput output) {
281+
SpringApplication application = new SpringApplication(ExampleConfig.class);
282+
application.setWebApplicationType(WebApplicationType.NONE);
283+
application.setAdditionalProfiles("p1,p2", "p3");
284+
application.run();
285+
assertThat(output).contains("The following 2 profiles are active: \"p1,p2\", \"p3\"");
265286
}
266287

267288
@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)