Dump Spring boot Configuration

前端 未结 3 516
别那么骄傲
别那么骄傲 2021-01-13 05:56

Our Ops guys want the Spring boot configuration (i.e. all properties) to be dumped to the log file when the app starts. I assume this can be done by injecting the properties

相关标签:
3条回答
  • 2021-01-13 06:50

    There is no built-in mechanism and it really depends what you mean by "all properties". Do you want only the actual keys that you wrote or you want all properties (including defaults).

    For the former, you could easily listen for ApplicationEnvironmentPreparedEvent and log the property sources you're interested in. For the latter, /configprops is indeed a much better/complete output.

    0 讨论(0)
  • 2021-01-13 06:52

    If you use actuator , env endpoint will give you all the configuration properties set in ConfigurableEnvironment and configprops will give you the list of @ConfigurationProperties, but not in the log.

    Take a look at the source code for this env endpoint, may be it will give you an idea of how you could get all the properties you are interested in.

    0 讨论(0)
  • 2021-01-13 06:53

    This logs only the properties configured *.properties file.

    /**
     * maps given property names to its origin
     * @return a map where key is property name and value the origin
     */
    public Map<String, String> fromWhere() {
        final Map<String, String> mapToLog = new HashMap<>();
    
        final MutablePropertySources propertySources = env.getPropertySources();
    
        final Iterator<?> it = propertySources.iterator();
    
        while (it.hasNext()) {
            final Object object = it.next();
            if (object instanceof MapPropertySource) {
                MapPropertySource propertySource = (MapPropertySource) object;
                String propertySourceName = propertySource.getName();
    
                if (propertySourceName.contains("properties")) {
    
                    Map<String, Object> sourceMap = propertySource.getSource();
    
                    for (String key : sourceMap.keySet()) {
                        final String envValue = env.getProperty(key);
                        String env2Val = System.getProperty(key);
    
                        String source = propertySource.getName().contains("file:") ? "FILE" : "JAR";
    
                        if (envValue.equals(env2Val)) {
                            source = "ENV";
                        }
    
                        mapToLog.putIfAbsent(key, source);               
                    }
                }
            }
        }
        return mapToLog;
    }
    

    my example output which depicts the property name, value and from where it comes. My property values are describing from where they come.:

    myprop: fooFromJar from JAR
    aPropFromFile: fromExternalConfFile from FILE
    mypropEnv: here from vm arg from ENV
    
    • ENV means that I have given it by -D to JVM.
    • JAR means it is from application.properties inside JAR
    • FILE means it is from application.properties outside JAR
    0 讨论(0)
提交回复
热议问题