From 6c805e634c91e95e637033c90595c92670ff9a70 Mon Sep 17 00:00:00 2001 From: RuanSheng Date: Tue, 21 Feb 2023 21:36:54 +0800 Subject: [PATCH] Fix nacos discovery/config log (#3133) (#3160) --- .../logging/NacosLoggingAppRunListener.java | 53 +++++++++++++++++++ .../nacos/logging/NacosLoggingListener.java | 1 + .../main/resources/META-INF/spring.factories | 4 +- .../logging/NacosLoggingListener.java | 1 + .../logging/NacosLoggingAppRunListener.java | 53 +++++++++++++++++++ .../main/resources/META-INF/spring.factories | 4 +- 6 files changed, 112 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 000000000..c5742ecf2 --- /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,53 @@ +/* + * 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.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(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 06d186b2a..65486bf78 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 @@ import org.springframework.core.ResolvableType; * * @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 a7ee06a44..498e763f2 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 @@ -8,5 +8,5 @@ com.alibaba.cloud.nacos.diagnostics.analyzer.NacosConnectionFailureAnalyzer 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 \ No newline at end of file +org.springframework.boot.SpringApplicationRunListener=\ +com.alibaba.cloud.nacos.logging.NacosLoggingAppRunListener \ No newline at end of file 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 ef23734e8..a0ae8fac2 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 @@ import org.springframework.core.ResolvableType; * * @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 000000000..c5742ecf2 --- /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,53 @@ +/* + * 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.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(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 460eb851b..aa7d28c21 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 @@ -11,5 +11,5 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.alibaba.cloud.nacos.util.UtilIPv6AutoConfiguration 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 \ No newline at end of file