-
-
Save carolusquintus/d422cf6d9a3962b5feda6b9abc07623a to your computer and use it in GitHub Desktop.
Spring Boot - Log all configuration properties on application startup
This file contains 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
import java.util.Arrays; | |
import java.util.List; | |
import java.util.stream.StreamSupport; | |
import lombok.extern.slf4j.Slf4j; | |
import org.springframework.context.annotation.Profile; | |
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; | |
@Slf4j | |
@Component | |
public class PropertyLogger { | |
/** | |
* Shows all properties of running environment.<br> | |
* See: <a href="https://gist.github.com/sandor-nemeth/f6d2899b714e017266cb9cce66bc719d">sandor-nemeth/PropertyLogger.java</a><br> | |
* | |
* @param event Event raised when an ApplicationContext gets initialized or refreshed. | |
*/ | |
@EventListener | |
public void handleContextRefresh(ContextRefreshedEvent event) { | |
final Environment env = event.getApplicationContext().getEnvironment(); | |
log.debug("====== Environment and configuration ======"); | |
log.debug("Active profiles: {}", Arrays.toString(env.getActiveProfiles())); | |
final MutablePropertySources sources = ((AbstractEnvironment) env).getPropertySources(); | |
final List<String> discardedProperties = | |
List.of("credentials", "password", "id", "secret", "key"); | |
StreamSupport.stream(sources.spliterator(), false) | |
.filter(ps -> ps instanceof EnumerablePropertySource) | |
.flatMap(ps -> Arrays.stream(((EnumerablePropertySource) ps).getPropertyNames())) | |
.distinct() | |
.filter( | |
prop -> discardedProperties | |
.stream() | |
.noneMatch(discarded -> prop.toLowerCase().contains(discarded)) | |
) | |
.forEach(prop -> log.debug(String.format("%-60s : %s", prop, env.getProperty(prop)))); | |
log.debug("====== Environment and configuration ======"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment