Skip to content

Commit 92cd51e

Browse files
committed
Polish "Clarify log message with a profile containing a comma"
See gh-29896
1 parent c8466a1 commit 92cd51e

File tree

3 files changed

+41
-27
lines changed

3 files changed

+41
-27
lines changed

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

Lines changed: 14 additions & 12 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,25 +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-
for (int i = 0; i < activeProfiles.length; i++) {
671-
activeProfiles[i] = "\"" + activeProfiles[i] + "\"";
672-
}
670+
List<String> activeProfiles = quoteProfiles(context.getEnvironment().getActiveProfiles());
673671
if (ObjectUtils.isEmpty(activeProfiles)) {
674-
String[] defaultProfiles = context.getEnvironment().getDefaultProfiles();
675-
for (int i = 0; i < defaultProfiles.length; i++) {
676-
defaultProfiles[i] = "\"" + defaultProfiles[i] + "\"";
677-
}
678-
log.info("No active profile set, falling back to " + defaultProfiles.length + " default profile(s): "
679-
+ 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, ", "));
680677
}
681678
else {
682-
log.info("The following " + activeProfiles.length + " profile(s) are active: "
683-
+ 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, ", "));
684682
}
685683
}
686684
}
687685

686+
private List<String> quoteProfiles(String[] profiles) {
687+
return Arrays.stream(profiles).map((profile) -> "\"" + profile + "\"").collect(Collectors.toList());
688+
}
689+
688690
/**
689691
* Returns the {@link Log} for the application. By default will be deduced.
690692
* @return the application log

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

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -249,37 +249,49 @@ 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 1 default profile(s): \"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");
261263
SpringApplication application = new SpringApplication(ExampleConfig.class);
262264
application.setWebApplicationType(WebApplicationType.NONE);
263-
this.context = application.run("--spring.profiles.active=myprofiles");
264-
assertThat(output).contains("The following 1 profile(s) are active: \"myprofiles\"");
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\"");
265269
}
266270

267271
@Test
268-
void enableBannerInLogViaProperty(CapturedOutput output) {
269-
SpringApplication application = spy(new SpringApplication(ExampleConfig.class));
272+
void logsActiveProfilesWithSingleProfile(CapturedOutput output) {
273+
SpringApplication application = new SpringApplication(ExampleConfig.class);
270274
application.setWebApplicationType(WebApplicationType.NONE);
271-
this.context = application.run("--spring.main.banner-mode=log");
272-
then(application).should(atLeastOnce()).setBannerMode(Banner.Mode.LOG);
273-
assertThat(output).contains("o.s.b.SpringApplication");
275+
this.context = application.run("--spring.profiles.active=myprofiles");
276+
assertThat(output).contains("The following 1 profile is active: \"myprofiles\"");
274277
}
275278

276279
@Test
277-
void logsMultipleActiveProfilesWithComma(CapturedOutput output) {
280+
void logsActiveProfilesWithMultipleProfiles(CapturedOutput output) {
278281
SpringApplication application = new SpringApplication(ExampleConfig.class);
279282
application.setWebApplicationType(WebApplicationType.NONE);
280283
application.setAdditionalProfiles("p1,p2", "p3");
281284
application.run();
282-
assertThat(output).contains("The following 2 profile(s) are active: \"p1,p2\",\"p3\"");
285+
assertThat(output).contains("The following 2 profiles are active: \"p1,p2\", \"p3\"");
286+
}
287+
288+
@Test
289+
void enableBannerInLogViaProperty(CapturedOutput output) {
290+
SpringApplication application = spy(new SpringApplication(ExampleConfig.class));
291+
application.setWebApplicationType(WebApplicationType.NONE);
292+
this.context = application.run("--spring.main.banner-mode=log");
293+
then(application).should(atLeastOnce()).setBannerMode(Banner.Mode.LOG);
294+
assertThat(output).contains("o.s.b.SpringApplication");
283295
}
284296

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