Skip to content

Commit 4ffcd3a

Browse files
committed
Log active profiles on SpringApplication.run
Fixes spring-projectsgh-3766
1 parent d1b936e commit 4ffcd3a

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import org.springframework.core.io.support.SpringFactoriesLoader;
6565
import org.springframework.util.Assert;
6666
import org.springframework.util.ClassUtils;
67+
import org.springframework.util.ObjectUtils;
6768
import org.springframework.util.ReflectionUtils;
6869
import org.springframework.util.StopWatch;
6970
import org.springframework.util.StringUtils;
@@ -335,6 +336,7 @@ private ConfigurableApplicationContext doRun(SpringApplicationRunListeners liste
335336
listeners.contextPrepared(context);
336337
if (this.logStartupInfo) {
337338
logStartupInfo(context.getParent() == null);
339+
logStartupProfileInfo(context);
338340
}
339341

340342
// Add boot specific singleton beans
@@ -627,6 +629,24 @@ protected void logStartupInfo(boolean isRoot) {
627629
}
628630
}
629631

632+
/**
633+
* Called to log active profile information.
634+
* @param context the application context
635+
*/
636+
protected void logStartupProfileInfo(ConfigurableApplicationContext context) {
637+
Log log = getApplicationLog();
638+
if (log.isInfoEnabled()) {
639+
String[] activeProfiles = context.getEnvironment().getActiveProfiles();
640+
if (ObjectUtils.isEmpty(activeProfiles)) {
641+
log.info("No profiles are active");
642+
}
643+
else {
644+
log.info("The following profiles are active: "
645+
+ StringUtils.arrayToCommaDelimitedString(activeProfiles));
646+
}
647+
}
648+
}
649+
630650
/**
631651
* Returns the {@link Log} for the application. By default will be deduced.
632652
* @return the application log

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
import org.springframework.util.StringUtils;
6868
import org.springframework.web.context.support.StandardServletEnvironment;
6969

70+
import static org.hamcrest.Matchers.containsString;
7071
import static org.hamcrest.Matchers.equalTo;
7172
import static org.hamcrest.Matchers.hasItem;
7273
import static org.hamcrest.Matchers.instanceOf;
@@ -195,6 +196,23 @@ public void customBannerWithProperties() throws Exception {
195196
startsWith(String.format("Running a Test!%n%n123456")));
196197
}
197198

199+
@Test
200+
public void logsNoActiveProfiles() throws Exception {
201+
SpringApplication application = new SpringApplication(ExampleConfig.class);
202+
application.setWebEnvironment(false);
203+
this.context = application.run();
204+
assertThat(this.output.toString(), containsString("No profiles are active"));
205+
}
206+
207+
@Test
208+
public void logsActiveProfiles() throws Exception {
209+
SpringApplication application = new SpringApplication(ExampleConfig.class);
210+
application.setWebEnvironment(false);
211+
this.context = application.run("--spring.profiles.active=myprofiles");
212+
assertThat(this.output.toString(),
213+
containsString("The following profiles are active: myprofile"));
214+
}
215+
198216
@Test
199217
public void customId() throws Exception {
200218
SpringApplication application = new SpringApplication(ExampleConfig.class);

0 commit comments

Comments
 (0)