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("==========================================="); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How can I use this code in the
public static void main(String[] args)
method on startup?