|
@@ -0,0 +1,79 @@
|
|
|
|
+package com.dragoninfo.dcuc.auth.filter;
|
|
|
|
+
|
|
|
|
+import com.ctrip.framework.apollo.Config;
|
|
|
|
+import com.ctrip.framework.apollo.model.ConfigChangeEvent;
|
|
|
|
+import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
|
|
|
|
+import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
|
|
|
|
+import org.springframework.beans.BeansException;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.boot.logging.LoggingInitializationContext;
|
|
|
|
+import org.springframework.boot.logging.LoggingSystem;
|
|
|
|
+import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
|
|
|
|
+import org.springframework.context.ApplicationContext;
|
|
|
|
+import org.springframework.context.ApplicationContextAware;
|
|
|
|
+import org.springframework.context.EnvironmentAware;
|
|
|
|
+import org.springframework.core.env.ConfigurableEnvironment;
|
|
|
|
+import org.springframework.core.env.Environment;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+import org.springframework.util.ResourceUtils;
|
|
|
|
+
|
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
|
+import java.util.Set;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @author huangzqa
|
|
|
|
+ * @date 2020/9/14
|
|
|
|
+ */
|
|
|
|
+@Component
|
|
|
|
+public class LoggingPropsRefresher implements ApplicationContextAware, EnvironmentAware {
|
|
|
|
+
|
|
|
|
+ private ApplicationContext applicationContext;
|
|
|
|
+
|
|
|
|
+ private ConfigurableEnvironment configurableEnvironment;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private LoggingSystem loggingSystem;
|
|
|
|
+
|
|
|
|
+ @ApolloConfig
|
|
|
|
+ private Config config;
|
|
|
|
+
|
|
|
|
+ @PostConstruct
|
|
|
|
+ private void initialize() {
|
|
|
|
+ if (config != null) {
|
|
|
|
+ refreshLoggingSystem(config.getPropertyNames());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 可升级新版本的 Apollo,使用 interestedKeyPrefixes
|
|
|
|
+ */
|
|
|
|
+ @ApolloConfigChangeListener(interestedKeys = {"dcuc.auth.zerotrust.syslog.host", "dcuc.auth.zerotrust.syslog.port", "dcuc.auth.zerotrust.syslog.facility"})
|
|
|
|
+ private void onChangeLog(ConfigChangeEvent configChangeEvent) {
|
|
|
|
+ refreshLoggingSystem(configChangeEvent.changedKeys());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
|
|
|
+ this.applicationContext = applicationContext;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void setEnvironment(Environment environment) {
|
|
|
|
+ this.configurableEnvironment = (ConfigurableEnvironment) environment;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void refreshLoggingSystem(Set<String> changeKeys) {
|
|
|
|
+ this.applicationContext.publishEvent(new EnvironmentChangeEvent(changeKeys));
|
|
|
|
+
|
|
|
|
+ LoggingInitializationContext initializationContext = new LoggingInitializationContext(this.configurableEnvironment);
|
|
|
|
+ String loggingConfig = configurableEnvironment.getProperty("logging.config");
|
|
|
|
+ try {
|
|
|
|
+ ResourceUtils.getURL(loggingConfig).openStream().close();
|
|
|
|
+ loggingSystem.cleanUp();
|
|
|
|
+ loggingSystem.initialize(initializationContext, loggingConfig, null);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|