Dump Spring boot Configuration
Asked Answered
S

4

7

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 with annotation @ConfigurationProperties and printing them.

The questions is whether there is a better or built-in mechanism to achieve this.

Given there does not seem to be a built in solution besides, I was try to cook my own. Here is what I came up with:

@Component
public class ConfigurationDumper {


    @Autowired
    public void init(Environment env){
        log.info("{}",env);
    }

}

The challenge with this is that it does not print variables that are in my application.yml. Instead, here is what I get:

StandardServletEnvironment 
{
    activeProfiles=[],
    defaultProfiles=[default],
    propertySources=[
        servletConfigInitParams,
        servletContextInitParams,
        systemProperties,
        systemEnvironment,
        random,
        applicationConfig: [classpath: /application.yml]
    ]
}

How can I fix this so as to have all properties loaded and printed?

Shaeffer answered 19/11, 2015 at 18:5 Comment(0)
E
5

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.

Edging answered 19/11, 2015 at 18:39 Comment(0)
L
2

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.

Lefthander answered 20/11, 2015 at 10:20 Comment(0)
N
2

If you consider actuator /env endpoint as a security risk, an alternative option is available at Baeldung's Log Properties in a Spring Boot (Maciej Główka). It consists in implementing an EventListener for ContextRefreshedEvent with following code:

env.getPropertySources()
    .stream()
    .filter(ps -> ps instanceof MapPropertySource)
    .map(ps -> ((MapPropertySource) ps).getSource().keySet())
    .flatMap(Collection::stream)
    .distinct()
    .sorted()
    .forEach(key -> LOGGER.info("{}={}", key, env.getProperty(key)));

You may also apply any filtering of interest, on PropertySource implementation or custom project's prefix for keys.

Nari answered 13/5, 2024 at 12:6 Comment(0)
K
0

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
Kant answered 8/9, 2020 at 7:11 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.