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