Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Map;
- import java.util.TreeMap;
- import org.jetbrains.annotations.NotNull;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
- import org.springframework.boot.context.event.ApplicationPreparedEvent;
- import org.springframework.context.ApplicationEvent;
- import org.springframework.context.ApplicationListener;
- import org.springframework.core.env.EnumerablePropertySource;
- import org.springframework.core.env.PropertySource;
- public class PropertiesLogger implements ApplicationListener<ApplicationEvent> {
- private static final Logger logger = LoggerFactory.getLogger(ConfigurationLogger.class);
- private Map<String, Object> configurationProperties = new TreeMap<>();
- @Override
- public void onApplicationEvent(@NotNull ApplicationEvent event) {
- if (event instanceof ApplicationEnvironmentPreparedEvent) {
- onApplicationEnvironmentPreparedEvent((ApplicationEnvironmentPreparedEvent) event);
- }else if( event instanceof ApplicationPreparedEvent){
- logProperties((ApplicationPreparedEvent)event);
- }
- }
- private void onApplicationEnvironmentPreparedEvent(ApplicationEnvironmentPreparedEvent event) {
- for (PropertySource<?> source : event.getEnvironment().getPropertySources()) {
- if (source instanceof EnumerablePropertySource) {
- for (String key : ((EnumerablePropertySource) source).getPropertyNames()) {
- Object value = source.getProperty(key);
- if (!configurationProperties.containsKey(key)) {
- configurationProperties.put(key, value);
- }
- }
- }
- }
- }
- private void logProperties(ApplicationPreparedEvent event) {
- logger.debug("Application started with following parameters: ");
- for( Map.Entry<String, Object> entry : configurationProperties.entrySet()){
- logger.debug("{} :: {}", entry.getKey(), entry.getValue());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement