Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* #11250
参考Log4J2NacosLogging处理,当location不存在时则不加载

* #11250
nacosLogbackConfigurators.stream()
                    .filter(c -> c.getVersion() == userVersion).collect(Collectors.toList())优化存在越界情况

* 调整异常输出

* #11250
移除无用注释代码

* #11250
调整代码风格样式

* #11250
默认加载方式为log4j,导致该测试用例异常;已修订

* #11250
按模板重新格式化代码
  • Loading branch information
llm163520 authored Nov 24, 2023
1 parent 736948f commit b50ccb0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="mailto:[email protected]">hxy1991</a>
* @author <a href="mailto:[email protected]">hujun</a>
*
* @since 0.9.0
*/
public class LogbackNacosLogging extends AbstractNacosLogging {
Expand All @@ -61,7 +61,7 @@ public void loadConfiguration() {
addListener(loggerContext);
}
}

private boolean hasListener(LoggerContext loggerContext) {
for (LoggerContextListener loggerContextListener : loggerContext.getCopyOfListenerList()) {
if (loggerContextListener instanceof NacosLoggerContextListener) {
Expand All @@ -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<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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

0 comments on commit b50ccb0

Please sign in to comment.