* 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
按模板重新格式化代码
This commit is contained in:
limliu 2023-11-24 17:01:12 +08:00 committed by GitHub
parent 736948f495
commit b50ccb0b15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 29 deletions

View File

@ -25,16 +25,16 @@ import com.alibaba.nacos.client.logging.AbstractNacosLogging;
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 <a href="mailto:huangxiaoyu1018@gmail.com">hxy1991</a>
* @author <a href="mailto:hujun3@xiaomi.com">hujun</a>
*
* @since 0.9.0
*/
public class LogbackNacosLogging extends AbstractNacosLogging {
@ -61,7 +61,7 @@ public class LogbackNacosLogging extends AbstractNacosLogging {
addListener(loggerContext);
}
}
private boolean hasListener(LoggerContext loggerContext) {
for (LoggerContextListener loggerContextListener : loggerContext.getCopyOfListenerList()) {
if (loggerContextListener instanceof NacosLoggerContextListener) {
@ -70,50 +70,55 @@ public class LogbackNacosLogging extends AbstractNacosLogging {
}
return false;
}
private LoggerContext loadConfigurationOnStart() {
String location = getLocation(NACOS_LOGBACK_LOCATION);
try {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
Collection<NacosLogbackConfigurator> 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<NacosLogbackConfigurator> 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());
}

View File

@ -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();
}
}
}