Log active profiles on SpringApplication.run

Fixes gh-3766
pull/4087/head
Phillip Webb 9 years ago
parent d1b936ef2c
commit 4ffcd3a22c

@ -64,6 +64,7 @@ import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.SpringFactoriesLoader; import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
import org.springframework.util.StopWatch; import org.springframework.util.StopWatch;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -335,6 +336,7 @@ public class SpringApplication {
listeners.contextPrepared(context); listeners.contextPrepared(context);
if (this.logStartupInfo) { if (this.logStartupInfo) {
logStartupInfo(context.getParent() == null); logStartupInfo(context.getParent() == null);
logStartupProfileInfo(context);
} }
// Add boot specific singleton beans // 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. * Returns the {@link Log} for the application. By default will be deduced.
* @return the application log * @return the application log

@ -67,6 +67,7 @@ import org.springframework.core.io.ResourceLoader;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.context.support.StandardServletEnvironment; import org.springframework.web.context.support.StandardServletEnvironment;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.instanceOf;
@ -195,6 +196,23 @@ public class SpringApplicationTests {
startsWith(String.format("Running a Test!%n%n123456"))); 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 @Test
public void customId() throws Exception { public void customId() throws Exception {
SpringApplication application = new SpringApplication(ExampleConfig.class); SpringApplication application = new SpringApplication(ExampleConfig.class);

Loading…
Cancel
Save