Log active profiles on SpringApplication.run
Fixes gh-3766
This commit is contained in:
parent
d1b936ef2c
commit
4ffcd3a22c
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Reference in New Issue