Skip to content

Instantly share code, notes, and snippets.

@sandor-nemeth
Last active April 23, 2024 13:50
Show Gist options
  • Star 88 You must be signed in to star a gist
  • Fork 23 You must be signed in to fork a gist
  • Save sandor-nemeth/f6d2899b714e017266cb9cce66bc719d to your computer and use it in GitHub Desktop.
Save sandor-nemeth/f6d2899b714e017266cb9cce66bc719d to your computer and use it in GitHub Desktop.
Spring Boot - Log all configuration properties on application startup
package io.github.sandornemeth.spring;
import java.util.Arrays;
import java.util.stream.StreamSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;
import org.springframework.stereotype.Component;
@Component
public class PropertyLogger {
private static final Logger LOGGER = LoggerFactory.getLogger(PropertyLogger.class);
@EventListener
public void handleContextRefresh(ContextRefreshedEvent event) {
final Environment env = event.getApplicationContext().getEnvironment();
LOGGER.info("====== Environment and configuration ======");
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)));
LOGGER.info("===========================================");
}
}
@mrossi975
Copy link

very useful, thanks a lot.
I improved it a bit by also calling .sorted() after .filter().

@gowtham789
Copy link

Perfect ! Its really useful.

@zhongzichang
Copy link

Thanks!

@jbournonville
Copy link

Perfect, thanks to you to have share this piece of code.

@abaines-phx
Copy link

Thanks 🚀

@abhishekgarg96
Copy link

But with this approach we are only being able to print the ENV variables and not the actual variables which are annotated with @value (edited)

@toudidel
Copy link

Very nice

@AhmedEzz
Copy link

Good one

@adamlarsonlee
Copy link

Hey this is great - thanks so much for posting. I'm using this to take application properties from Spring and map them into system properties that are used by an embedded third party application.

@christophknabe
Copy link

By that we can see what property values are really in effect when running the application. I used it with some modifications to see the property values in effect when running a test case. Thank you very much.

@mxmlnglt
Copy link

Doesn't work if there is a configuration problem (e.g. a bean can't initialize due to a missing property, then the APPLICATION FAILED TO START); it's not even executed...

@Clcanny
Copy link

Clcanny commented Feb 14, 2022

Thank you!

@domenecsos
Copy link

Doesn't work if there is a configuration problem (e.g. a bean can't initialize due to a missing property, then the APPLICATION FAILED TO START); it's not even executed...

Worked for me with Spring Boot 2.6.3.

I created a placeholder for a non existant property @Value("${pepe}") private String pepe; and got this.

17:10:59.488 [main] ERROR o.s.boot.SpringApplication - Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'generalInfo': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'pepe' in value "${pepe}"
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145)
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'pepe' in value "${pepe}"

@mxmlnglt
Copy link

mxmlnglt commented Apr 7, 2022

Worked for me with Spring Boot 2.6.3.

OK great then!! the project I was working on used v2.5.3 so seems it's been fixed since then...

@omerhakanbilici
Copy link

you saved the day! thanks

@ayakimchuk322
Copy link

Works like a charm with Spring Boot 2.7.3

@Christian-Oette
Copy link

Works great. But when the application crashes, it is not called, as already mentioned. This can be fixed, with a listener, which is manually added to the spring application. See here
https://gist.github.com/Christian-Oette/a0bb05cb703fea20d28bbf46b7ce7574

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment