Просмотр исходного кода

[task-842] 打印系统变量和参数设置

Li Yuan 2 лет назад
Родитель
Сommit
45c4dee947

+ 71 - 0
ibps-basic-root/modules/basic-response/src/main/java/com/lc/ibps/cloud/Listener/LoggingListener.java

@@ -0,0 +1,71 @@
+package com.lc.ibps.cloud.Listener;
+
+import org.apache.commons.lang.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.boot.context.event.ApplicationPreparedEvent;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationListener;
+import org.springframework.core.env.AbstractEnvironment;
+import org.springframework.core.env.EnumerablePropertySource;
+import org.springframework.core.env.Environment;
+import org.springframework.core.env.MutablePropertySources;
+import org.springframework.core.env.PropertySource;
+import org.springframework.stereotype.Component;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.StreamSupport;
+
+@Component
+public class LoggingListener implements ApplicationListener<ApplicationPreparedEvent> {
+    private final List<String> SENSITIVE_PROPERTIES = Arrays.asList("password", "credential", "token");
+    private static Logger log = LoggerFactory.getLogger(LoggingListener.class);
+    @Override
+    public void onApplicationEvent(final ApplicationPreparedEvent event) {
+        logProperties(event.getApplicationContext());
+    }
+
+    public void logProperties(ApplicationContext applicationContext) {
+        final Environment env = applicationContext.getEnvironment();
+        StringBuilder stringBuilder = new StringBuilder();
+        stringBuilder.append("\n====== Log all properties ======\n")
+                .append("Active profiles: ")
+                .append(Arrays.toString(env.getActiveProfiles()))
+                .append("\n");
+
+        if (env instanceof AbstractEnvironment) {
+            final MutablePropertySources sources = ((AbstractEnvironment) env).getPropertySources();
+            StreamSupport.stream(sources.spliterator(), false)
+                    .filter(ps -> ps instanceof EnumerablePropertySource)
+                    .map(this::castToEnumerablePropertySource)
+                    .map(EnumerablePropertySource::getPropertyNames)
+                    .flatMap(Arrays::stream)
+                    .forEach(prop -> appendProperty(env, prop, stringBuilder));
+        } else {
+            stringBuilder.append("Unable to read properties");
+        }
+        log.error(stringBuilder.toString());
+    }
+
+    private EnumerablePropertySource<?> castToEnumerablePropertySource(final PropertySource<?> propertySource) {
+        return (EnumerablePropertySource<?>) propertySource;
+    }
+
+    private void appendProperty(final Environment env, final String prop, final StringBuilder stringBuilder) {
+        boolean isASensitiveProperty = SENSITIVE_PROPERTIES.stream()
+                .anyMatch(prop::contains);
+        try {
+            final String value;
+            if (isASensitiveProperty) {
+                value = StringUtils.abbreviate(env.getProperty(prop), 4);
+            } else {
+                value = env.getProperty(prop);
+            }
+            stringBuilder.append(String.format("%s: %s\n", prop, value));
+        } catch (Exception ex) {
+            stringBuilder.append(String.format("%s: %s\n", prop, ex.getMessage()));
+        }
+
+    }
+}