1. Introduction
In this short article, we will present how to log all system properties on Spring Boot startup.
2. The org.springframework.core.env.Environment
interface
In Spring Boot applications the Environment
is an interface representing the environment in which the current application is running.
We can use that interface to get active profiles and properties of the application environment.
The following code will print all properties from the environment:
package com.frontbackend.springboot;
import java.util.Arrays;
import java.util.stream.StreamSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MutablePropertySources;
@SpringBootApplication
public class GettingStartedApplication {
private static final Logger LOGGER = LoggerFactory.getLogger(GettingStartedApplication.class);
public static void main(String[] args) {
SpringApplication.run(GettingStartedApplication.class, args);
}
@EventListener
public void handleContextRefresh(ContextRefreshedEvent event) {
final Environment env = event.getApplicationContext()
.getEnvironment();
LOGGER.info("Active profiles: {}", Arrays.toString(env.getActiveProfiles()));
final MutablePropertySources sources = ((AbstractEnvironment) env).getPropertySources();
StreamSupport.stream(sources.spliterator(), false)
.filter(ps -> ps instanceof EnumerablePropertySource)
.map(ps -> ((EnumerablePropertySource) ps).getPropertyNames())
.flatMap(Arrays::stream)
.distinct()
.filter(prop -> !(prop.contains("credentials") || prop.contains("password")))
.forEach(prop -> LOGGER.info("{}: {}", prop, env.getProperty(prop)));
}
}
Note that the Environment
object could be autowired in Spring Boot components just like other Spring beans.
The following code prints several environment properties on startup:
package com.frontbackend.springboot;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;
@SpringBootApplication
public class GettingStartedApplication implements CommandLineRunner {
private static final Logger logger = LoggerFactory.getLogger(GettingStartedApplication.class);
@Autowired
private Environment env;
@Override
public void run(String... args) {
logger.info("{}", env.getProperty("file.encoding"));
logger.info("{}", env.getProperty("java.io.tmpdir"));
logger.info("{}", env.getProperty("os.name"));
logger.info("{}", env.getProperty("app.name"));
}
public static void main(String[] args) {
SpringApplication.run(GettingStartedApplication.class, args);
}
}
3. Conclusion
In this short article, we presented how to print all the important environment information on Spring Boot startup.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}