From 4ffcd3a22c401f66afdf9f3a99da1144c31bff89 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 2 Oct 2015 16:08:03 -0700 Subject: [PATCH] Log active profiles on SpringApplication.run Fixes gh-3766 --- .../boot/SpringApplication.java | 20 +++++++++++++++++++ .../boot/SpringApplicationTests.java | 18 +++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java index b9bba8ca0b..6c8c2af65a 100644 --- a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -64,6 +64,7 @@ import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.support.SpringFactoriesLoader; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; +import org.springframework.util.ObjectUtils; import org.springframework.util.ReflectionUtils; import org.springframework.util.StopWatch; import org.springframework.util.StringUtils; @@ -335,6 +336,7 @@ public class SpringApplication { listeners.contextPrepared(context); if (this.logStartupInfo) { logStartupInfo(context.getParent() == null); + logStartupProfileInfo(context); } // Add boot specific singleton beans @@ -627,6 +629,24 @@ public class SpringApplication { } } + /** + * Called to log active profile information. + * @param context the application context + */ + protected void logStartupProfileInfo(ConfigurableApplicationContext context) { + Log log = getApplicationLog(); + if (log.isInfoEnabled()) { + String[] activeProfiles = context.getEnvironment().getActiveProfiles(); + if (ObjectUtils.isEmpty(activeProfiles)) { + log.info("No profiles are active"); + } + else { + log.info("The following profiles are active: " + + StringUtils.arrayToCommaDelimitedString(activeProfiles)); + } + } + } + /** * Returns the {@link Log} for the application. By default will be deduced. * @return the application log diff --git a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java b/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java index 875d9f478e..e0284c77d5 100644 --- a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java @@ -67,6 +67,7 @@ import org.springframework.core.io.ResourceLoader; import org.springframework.util.StringUtils; import org.springframework.web.context.support.StandardServletEnvironment; +import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.instanceOf; @@ -195,6 +196,23 @@ public class SpringApplicationTests { startsWith(String.format("Running a Test!%n%n123456"))); } + @Test + public void logsNoActiveProfiles() throws Exception { + SpringApplication application = new SpringApplication(ExampleConfig.class); + application.setWebEnvironment(false); + this.context = application.run(); + assertThat(this.output.toString(), containsString("No profiles are active")); + } + + @Test + public void logsActiveProfiles() throws Exception { + SpringApplication application = new SpringApplication(ExampleConfig.class); + application.setWebEnvironment(false); + this.context = application.run("--spring.profiles.active=myprofiles"); + assertThat(this.output.toString(), + containsString("The following profiles are active: myprofile")); + } + @Test public void customId() throws Exception { SpringApplication application = new SpringApplication(ExampleConfig.class);