From d5558a9d2c68a0f91ff82db9dbde71dad96be7bd Mon Sep 17 00:00:00 2001 From: RuanSheng Date: Wed, 22 Feb 2023 17:50:44 +0800 Subject: [PATCH] [2022.x] Fix nacos discovery/config log (#3133) (#3160) (#3166) --- .../logging/NacosLoggingAppRunListener.java | 54 +++++++++++++++++++ .../nacos/logging/NacosLoggingListener.java | 1 + .../main/resources/META-INF/spring.factories | 4 +- .../logging/NacosLoggingListener.java | 1 + .../logging/NacosLoggingAppRunListener.java | 54 +++++++++++++++++++ .../main/resources/META-INF/spring.factories | 4 +- 6 files changed, 114 insertions(+), 4 deletions(-) create mode 100644 spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/logging/NacosLoggingAppRunListener.java create mode 100644 spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/logging/NacosLoggingAppRunListener.java diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/logging/NacosLoggingAppRunListener.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/logging/NacosLoggingAppRunListener.java new file mode 100644 index 0000000000..e6f170c1d3 --- /dev/null +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/logging/NacosLoggingAppRunListener.java @@ -0,0 +1,54 @@ +/* + * Copyright 2013-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.cloud.nacos.logging; + +import com.alibaba.nacos.client.logging.NacosLogging; + +import org.springframework.boot.ConfigurableBootstrapContext; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.SpringApplicationRunListener; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.Ordered; +import org.springframework.core.env.ConfigurableEnvironment; + +/** + * Reload nacos log configuration on spring application contextPrepared. + * + * @author RuanSheng + */ +public class NacosLoggingAppRunListener implements SpringApplicationRunListener, Ordered { + + public NacosLoggingAppRunListener(SpringApplication application, String[] args) { + + } + + @Override + public void environmentPrepared(ConfigurableBootstrapContext bootstrapContext, ConfigurableEnvironment environment) { + NacosLogging.getInstance().loadConfiguration(); + } + + @Override + public void contextPrepared(ConfigurableApplicationContext context) { + NacosLogging.getInstance().loadConfiguration(); + } + + @Override + public int getOrder() { + return 1; + } + +} diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/logging/NacosLoggingListener.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/logging/NacosLoggingListener.java index 06d186b2a8..65486bf780 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/logging/NacosLoggingListener.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/logging/NacosLoggingListener.java @@ -30,6 +30,7 @@ * * @author mai.jh */ +@Deprecated public class NacosLoggingListener implements GenericApplicationListener { @Override diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/resources/META-INF/spring.factories b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/resources/META-INF/spring.factories index 9571a34854..381d44bab9 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/resources/META-INF/spring.factories @@ -6,8 +6,8 @@ com.alibaba.cloud.nacos.configdata.NacosConfigDataMissingEnvironmentPostProcesso org.springframework.boot.env.PropertySourceLoader=\ com.alibaba.cloud.nacos.parser.NacosJsonPropertySourceLoader,\ com.alibaba.cloud.nacos.parser.NacosXmlPropertySourceLoader -org.springframework.context.ApplicationListener=\ -com.alibaba.cloud.nacos.logging.NacosLoggingListener +org.springframework.boot.SpringApplicationRunListener=\ +com.alibaba.cloud.nacos.logging.NacosLoggingAppRunListener org.springframework.boot.env.EnvironmentPostProcessor=\ com.alibaba.cloud.nacos.configdata.NacosConfigDataMissingEnvironmentPostProcessor diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/discovery/logging/NacosLoggingListener.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/discovery/logging/NacosLoggingListener.java index ef23734e81..a0ae8fac27 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/discovery/logging/NacosLoggingListener.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/discovery/logging/NacosLoggingListener.java @@ -30,6 +30,7 @@ * * @author mai.jh */ +@Deprecated public class NacosLoggingListener implements GenericApplicationListener { @Override diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/logging/NacosLoggingAppRunListener.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/logging/NacosLoggingAppRunListener.java new file mode 100644 index 0000000000..e6f170c1d3 --- /dev/null +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/logging/NacosLoggingAppRunListener.java @@ -0,0 +1,54 @@ +/* + * Copyright 2013-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.cloud.nacos.logging; + +import com.alibaba.nacos.client.logging.NacosLogging; + +import org.springframework.boot.ConfigurableBootstrapContext; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.SpringApplicationRunListener; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.Ordered; +import org.springframework.core.env.ConfigurableEnvironment; + +/** + * Reload nacos log configuration on spring application contextPrepared. + * + * @author RuanSheng + */ +public class NacosLoggingAppRunListener implements SpringApplicationRunListener, Ordered { + + public NacosLoggingAppRunListener(SpringApplication application, String[] args) { + + } + + @Override + public void environmentPrepared(ConfigurableBootstrapContext bootstrapContext, ConfigurableEnvironment environment) { + NacosLogging.getInstance().loadConfiguration(); + } + + @Override + public void contextPrepared(ConfigurableApplicationContext context) { + NacosLogging.getInstance().loadConfiguration(); + } + + @Override + public int getOrder() { + return 1; + } + +} diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/resources/META-INF/spring.factories b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/resources/META-INF/spring.factories index 9d85a5579b..b5b313373c 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/resources/META-INF/spring.factories @@ -1,4 +1,4 @@ org.springframework.cloud.bootstrap.BootstrapConfiguration=\ com.alibaba.cloud.nacos.discovery.configclient.NacosDiscoveryClientConfigServiceBootstrapConfiguration -org.springframework.context.ApplicationListener=\ - com.alibaba.cloud.nacos.discovery.logging.NacosLoggingListener +org.springframework.boot.SpringApplicationRunListener=\ + com.alibaba.cloud.nacos.logging.NacosLoggingAppRunListener