79424981

Date: 2025-02-09 13:42:32
Score: 1.5
Natty:
Report link

I originally came here because I had the same question.

Yes, logging.level.org.springframework.boot.context.config: trace is a good answer, but it's verbose: its output contains much more than this question asks for.

I'm not complaining. The additional information, such as which files would have been loaded if they'd existed ("Skipping missing resource file"), is interesting to me.

Still, for what it's worth, if you want something closer to just the "list of loaded properties files":

@Component
@Slf4j
public class ConfigFileLogger {
    @EventListener
    public void handleContextRefresh(ContextRefreshedEvent event) {
        final Environment env = event.getApplicationContext().getEnvironment();
        final MutablePropertySources sources = ((AbstractEnvironment) env).getPropertySources();
        sources.stream()
            .filter(ps -> ps instanceof OriginTrackedMapPropertySource)
            .forEach(ps -> log.info("{}", ps.getName()));
    }
}

Example output:

Config resource 'file [config/application.yaml]' via location 'config/application.yaml'

Yeah: that whole line, "Config resource ... via location ...", is the name of the property source.

Before you say, "Well, I'm just gonna use a regex to extract the location from that line", here's another example, for a config file that is embedded in the Spring Boot application JAR file:

Config resource 'class path resource [application.yaml]' via location 'optional:classpath:/'

If anyone knows a better, "cleaner" way to get a list of file paths that offers the same context about the location, please post your answer.

OriginTrackedMapPropertySource ?

In case you're wondering how I knew to filter property sources by instanceof OriginTrackedMapPropertySource, I first listed all property sources:

sources.stream()
    .forEach(ps -> log.info("{}", ps));

Example output:

ConfigurationPropertySourcesPropertySource {name='configurationProperties'}
SimpleCommandLinePropertySource {name='commandLineArgs'}
PropertiesPropertySource {name='systemProperties'}
OriginAwareSystemEnvironmentPropertySource {name='systemEnvironment'}
RandomValuePropertySource {name='random'}
OriginTrackedMapPropertySource {name='Config resource 'file [config/application.yaml]' via location 'config/application.yaml''}
OriginTrackedMapPropertySource {name='Config resource 'class path resource [application.yaml]' via location 'optional:classpath:/''}
Reasons:
  • Blacklisted phrase (1): anyone knows
  • Whitelisted phrase (-1): I had the same
  • RegEx Blacklisted phrase (2.5): please post your answer
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
Posted by: Graham Hannington