Last active
April 5, 2025 17:46
-
-
Save sandor-nemeth/f6d2899b714e017266cb9cce66bc719d to your computer and use it in GitHub Desktop.
Spring Boot - Log all configuration properties on application startup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("==========================================="); | |
} | |
} |
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...
you saved the day! thanks
Works like a charm with Spring Boot 2.7.3
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
How can I use this code in the public static void main(String[] args)
method on startup?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.