-
-
Save mbaykara/17f9393e0e9cec51ddcc6b3a331f382c 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
//NOT MY CODE | |
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("==========================================="); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment