Reputation: 2190
Is there anyway I can know in my program, the full path of file loaded through @PropertySource annotation of Spring. I need it to show in logs so that one can know which property file is being used in the application
Upvotes: 4
Views: 3135
Reputation: 1562
in current ('21) versions of Spring Boot, neither of the two above suggestions for the logging level seem to work. moreover - if the file is actually NOT loaded, because it is NOT found or for whatever other reason, nothing is printed anyway.
at the moment when i have my ROOT logger set to DEBUG (logging.level.root=DEBUG in application.properties), the only thing I see in the log file, when the file is loaded correctly and the @Value annotated property is resolved successfully is:
2021-07-23 11:06:10.299 DEBUG 16776 --- [ restartedMain] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'bahblahService'
2021-07-23 11:06:10.302 DEBUG 16776 --- [ restartedMain] o.s.c.e.PropertySourcesPropertyResolver : Found key 'blahblah.username' in PropertySource 'class path resource [custom-local.properties]' with value of type String
Upvotes: 0
Reputation: 15244
Below seems to be working, though I am not sure if the instance is always of type ConfigurableEnvironment
@Component
public class MyListener implements ApplicationListener<ContextRefreshedEvent>{
@Autowired
private Environment env;
private static final Logger log = LoggerFactory.getLogger(MyListener.class);
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
if(env instanceof ConfigurableEnvironment){
MutablePropertySources propertySources = ((ConfigurableEnvironment)env).getPropertySources();
for(PropertySource ps : propertySources){
log.info(ps.getName()); //if only file based needed then check if instanceof ResourcePropertySource
}
}
}
}
Edit: don't really need all this. As already answered by Selim, enabling the proper logs does the trick
log4j.logger.org.springframework.core.env.MutablePropertySources=DEBUG
Upvotes: 3
Reputation: 1161
This information is logged already by StandardServletEnvironment. You can set log level to DEBUG for org.springframework.web.context.support.StandardServletEnvironment class to show details in your logs.
If you use spring-boot you can simply add following line into your application.properties file.
logging.level.org.springframework.web.context.support.StandardServletEnvironment = DEBUG
Upvotes: 4