Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. import java.util.Map;
  2. import java.util.TreeMap;
  3. import org.jetbrains.annotations.NotNull;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
  7. import org.springframework.boot.context.event.ApplicationPreparedEvent;
  8. import org.springframework.context.ApplicationEvent;
  9. import org.springframework.context.ApplicationListener;
  10. import org.springframework.core.env.EnumerablePropertySource;
  11. import org.springframework.core.env.PropertySource;
  12.  
  13. public class PropertiesLogger implements ApplicationListener<ApplicationEvent> {
  14. private static final Logger logger = LoggerFactory.getLogger(ConfigurationLogger.class);
  15. private Map<String, Object> configurationProperties = new TreeMap<>();
  16.  
  17. @Override
  18. public void onApplicationEvent(@NotNull ApplicationEvent event) {
  19. if (event instanceof ApplicationEnvironmentPreparedEvent) {
  20. onApplicationEnvironmentPreparedEvent((ApplicationEnvironmentPreparedEvent) event);
  21. }else if( event instanceof ApplicationPreparedEvent){
  22. logProperties((ApplicationPreparedEvent)event);
  23. }
  24. }
  25.  
  26. private void onApplicationEnvironmentPreparedEvent(ApplicationEnvironmentPreparedEvent event) {
  27. for (PropertySource<?> source : event.getEnvironment().getPropertySources()) {
  28. if (source instanceof EnumerablePropertySource) {
  29. for (String key : ((EnumerablePropertySource) source).getPropertyNames()) {
  30. Object value = source.getProperty(key);
  31. if (!configurationProperties.containsKey(key)) {
  32. configurationProperties.put(key, value);
  33. }
  34. }
  35. }
  36. }
  37. }
  38.  
  39. private void logProperties(ApplicationPreparedEvent event) {
  40. logger.debug("Application started with following parameters: ");
  41. for( Map.Entry<String, Object> entry : configurationProperties.entrySet()){
  42. logger.debug("{} :: {}", entry.getKey(), entry.getValue());
  43. }
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement