Skip to content

Instantly share code, notes, and snippets.

@thermosym
Forked from sandor-nemeth/PropertyLogger.java
Created March 6, 2019 09:37
Show Gist options
  • Save thermosym/effc3b1f09a11205af781772e38b9c85 to your computer and use it in GitHub Desktop.
Save thermosym/effc3b1f09a11205af781772e38b9c85 to your computer and use it in GitHub Desktop.
Spring Boot - Log all configuration properties on application startup
package de.idealo.ecommerce.order.history.config;
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