How to log the active configuration in a Spring Boot application?

前端 未结 5 1513
野性不改
野性不改 2020-12-14 15:51

I would really like to use YAML config for Spring Boot, as I find it quite readable and useful to have a single file showing what properties are active in my different profi

5条回答
  •  离开以前
    2020-12-14 16:55

    In addition to other answers: logging active properties on context refreshed event.

    Java 8

    package mypackage;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.context.event.ContextRefreshedEvent;
    import org.springframework.context.event.EventListener;
    import org.springframework.core.env.ConfigurableEnvironment;
    import org.springframework.core.env.MapPropertySource;
    import org.springframework.stereotype.Component;
    
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.List;
    
    @Slf4j
    @Component
    public class AppContextEventListener {
    
        @EventListener
        public void handleContextRefreshed(ContextRefreshedEvent event) {
            printActiveProperties((ConfigurableEnvironment) event.getApplicationContext().getEnvironment());
        }
    
        private void printActiveProperties(ConfigurableEnvironment env) {
    
            System.out.println("************************* ACTIVE APP PROPERTIES ******************************");
    
            List propertySources = new ArrayList<>();
    
            env.getPropertySources().forEach(it -> {
                if (it instanceof MapPropertySource && it.getName().contains("applicationConfig")) {
                    propertySources.add((MapPropertySource) it);
                }
            });
    
            propertySources.stream()
                    .map(propertySource -> propertySource.getSource().keySet())
                    .flatMap(Collection::stream)
                    .distinct()
                    .sorted()
                    .forEach(key -> {
                        try {
                            System.out.println(key + "=" + env.getProperty(key));
                        } catch (Exception e) {
                            log.warn("{} -> {}", key, e.getMessage());
                        }
                    });
            System.out.println("******************************************************************************");
        }
    }
    

    Kotlin

    package mypackage
    
    import mu.KLogging
    import org.springframework.context.event.ContextRefreshedEvent
    import org.springframework.context.event.EventListener
    import org.springframework.core.env.ConfigurableEnvironment
    import org.springframework.core.env.MapPropertySource
    import org.springframework.stereotype.Component
    
    @Component
    class AppContextEventListener {
    
        companion object : KLogging()
    
        @EventListener
        fun handleContextRefreshed(event: ContextRefreshedEvent) {
            printActiveProperties(event.applicationContext.environment as ConfigurableEnvironment)
        }
    
        fun printActiveProperties(env: ConfigurableEnvironment) {
            println("************************* ACTIVE APP PROPERTIES ******************************")
            env.propertySources
                    .filter { it.name.contains("applicationConfig") }
                    .map { it as EnumerablePropertySource<*> }
                    .map { it -> it.propertyNames.toList() }
                    .flatMap { it }
                    .distinctBy { it }
                    .sortedBy { it }
                    .forEach { it ->
                        try {
                            println("$it=${env.getProperty(it)}")
                        } catch (e: Exception) {
                            logger.warn("$it -> ${e.message}")
                        }
                    }
            println("******************************************************************************")
        }
    }
    

    Output like:

    ************************* ACTIVE APP PROPERTIES ******************************
    server.port=3000
    spring.application.name=my-app
    ...
    2017-12-29 13:13:32.843  WARN 36252 --- [           main] m.AppContextEventListener        : spring.boot.admin.client.service-url -> Could not resolve placeholder 'management.address' in value "http://${management.address}:${server.port}"
    ...
    spring.datasource.password=
    spring.datasource.url=jdbc:postgresql://localhost/my_db?currentSchema=public
    spring.datasource.username=db_user
    ...
    ******************************************************************************
    

提交回复
热议问题