From b50ccb0b159347f18a5bdf9d0bfc4329f4ce91af Mon Sep 17 00:00:00 2001 From: limliu <261010267@qq.com> Date: Fri, 24 Nov 2023 17:01:12 +0800 Subject: [PATCH] https://github.com/alibaba/nacos/issues/11250 (#11251) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * https://github.com/alibaba/nacos/issues/11250 参考Log4J2NacosLogging处理,当location不存在时则不加载 * https://github.com/alibaba/nacos/issues/11250 nacosLogbackConfigurators.stream() .filter(c -> c.getVersion() == userVersion).collect(Collectors.toList())优化存在越界情况 * 调整异常输出 * https://github.com/alibaba/nacos/issues/11250 移除无用注释代码 * https://github.com/alibaba/nacos/issues/11250 调整代码风格样式 * https://github.com/alibaba/nacos/issues/11250 默认加载方式为log4j,导致该测试用例异常;已修订 * https://github.com/alibaba/nacos/issues/11250 按模板重新格式化代码 --- .../logging/logback/LogbackNacosLogging.java | 55 ++++++++++--------- .../logback/LogbackNacosLoggingTest.java | 14 +++-- 2 files changed, 40 insertions(+), 29 deletions(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/logging/logback/LogbackNacosLogging.java b/client/src/main/java/com/alibaba/nacos/client/logging/logback/LogbackNacosLogging.java index 9c63da36675..e67c56f980a 100644 --- a/client/src/main/java/com/alibaba/nacos/client/logging/logback/LogbackNacosLogging.java +++ b/client/src/main/java/com/alibaba/nacos/client/logging/logback/LogbackNacosLogging.java @@ -25,16 +25,16 @@ import com.alibaba.nacos.common.log.NacosLogbackConfigurator; import com.alibaba.nacos.common.spi.NacosServiceLoader; import com.alibaba.nacos.common.utils.ResourceUtils; +import com.alibaba.nacos.common.utils.StringUtils; import org.slf4j.LoggerFactory; + import java.util.Collection; -import java.util.stream.Collectors; /** * Support for Logback version 1.0.8 or higher * * @author hxy1991 * @author hujun - * * @since 0.9.0 */ public class LogbackNacosLogging extends AbstractNacosLogging { @@ -61,7 +61,7 @@ public void loadConfiguration() { addListener(loggerContext); } } - + private boolean hasListener(LoggerContext loggerContext) { for (LoggerContextListener loggerContextListener : loggerContext.getCopyOfListenerList()) { if (loggerContextListener instanceof NacosLoggerContextListener) { @@ -70,50 +70,55 @@ private boolean hasListener(LoggerContext loggerContext) { } return false; } - + private LoggerContext loadConfigurationOnStart() { String location = getLocation(NACOS_LOGBACK_LOCATION); - try { - LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); - Collection nacosLogbackConfigurators = NacosServiceLoader.load( - NacosLogbackConfigurator.class); - NacosLogbackConfigurator nacosLogbackConfigurator = nacosLogbackConfigurators.stream() - .filter(c -> c.getVersion() == userVersion).collect(Collectors.toList()).get(0); - nacosLogbackConfigurator.setContext(loggerContext); - nacosLogbackConfigurator.configure(ResourceUtils.getResourceUrl(location)); - return loggerContext; - } catch (Exception e) { - throw new IllegalStateException("Could not initialize Logback Nacos logging from " + location, e); - } + LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); + Collection nacosLogbackConfigurators = NacosServiceLoader.load( + NacosLogbackConfigurator.class); + nacosLogbackConfigurators.stream().filter(c -> c.getVersion() == userVersion).findFirst() + .ifPresent(nacosLogbackConfigurator -> { + nacosLogbackConfigurator.setContext(loggerContext); + if (StringUtils.isNotBlank(location)) { + try { + nacosLogbackConfigurator.configure(ResourceUtils.getResourceUrl(location)); + } catch (Exception e) { + throw new IllegalStateException( + "Could not initialize Logback Nacos logging from " + location, e); + } + } + }); + return loggerContext; } - + class NacosLoggerContextListener implements LoggerContextListener { + @Override public boolean isResetResistant() { return true; } - + @Override public void onReset(LoggerContext context) { loadConfigurationOnStart(); } - + @Override public void onStart(LoggerContext context) { - + } - + @Override public void onStop(LoggerContext context) { - + } - + @Override public void onLevelChange(Logger logger, Level level) { - + } } - + private void addListener(LoggerContext loggerContext) { loggerContext.addListener(new NacosLoggerContextListener()); } diff --git a/client/src/test/java/com/alibaba/nacos/client/logging/logback/LogbackNacosLoggingTest.java b/client/src/test/java/com/alibaba/nacos/client/logging/logback/LogbackNacosLoggingTest.java index 6822f2c6121..274af69b3d9 100644 --- a/client/src/test/java/com/alibaba/nacos/client/logging/logback/LogbackNacosLoggingTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/logging/logback/LogbackNacosLoggingTest.java @@ -18,9 +18,12 @@ package com.alibaba.nacos.client.logging.logback; +import ch.qos.logback.classic.LoggerContext; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.slf4j.ILoggerFactory; +import org.slf4j.LoggerFactory; import static org.hamcrest.CoreMatchers.isA; @@ -31,9 +34,12 @@ public class LogbackNacosLoggingTest { @Test public void testLoadConfiguration() { - exceptionRule.expectCause(isA(ClassCastException.class)); - exceptionRule.expectMessage("Could not initialize Logback Nacos logging from classpath:nacos-logback.xml"); - LogbackNacosLogging logbackNacosLogging = new LogbackNacosLogging(); - logbackNacosLogging.loadConfiguration(); + ILoggerFactory loggerFactory; + if ((loggerFactory = LoggerFactory.getILoggerFactory()) != null && loggerFactory instanceof LoggerContext) { + exceptionRule.expectCause(isA(ClassCastException.class)); + exceptionRule.expectMessage("Could not initialize Logback Nacos logging from classpath:nacos-logback.xml"); + LogbackNacosLogging logbackNacosLogging = new LogbackNacosLogging(); + logbackNacosLogging.loadConfiguration(); + } } } \ No newline at end of file