[ISSUE#11956] Fix logging module old bugs (#12011)

* Remove log4j dependencies from nacos-client.

* Fix logback listener don't add to context.

* Fix can't adapter low version logback below 1.1.10.

* For checkstyle.
This commit is contained in:
杨翊 SionYang 2024-04-24 18:07:34 +08:00 committed by GitHub
parent de10c07146
commit da3e88b3d4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 21 additions and 22 deletions

View File

@ -72,24 +72,6 @@
<artifactId>nacos-log4j2-adapter</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<artifactId>commons-codec</artifactId>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 1999-2018 Alibaba Group Holding Ltd.
~ Copyright 1999-2023 Alibaba Group Holding Ltd.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.

View File

@ -20,7 +20,6 @@ import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.LoggerContextListener;
import ch.qos.logback.core.CoreConstants;
import com.alibaba.nacos.common.logging.NacosLoggingAdapter;
import com.alibaba.nacos.common.logging.NacosLoggingProperties;
import com.alibaba.nacos.common.utils.ResourceUtils;
@ -91,7 +90,7 @@ public class LogbackNacosLoggingAdapter implements NacosLoggingAdapter {
String location = loggingProperties.getLocation();
configurator.setLoggingProperties(loggingProperties);
LoggerContext loggerContext = loadConfigurationOnStart(location);
if (loggerContext.getObject(CoreConstants.RECONFIGURE_ON_CHANGE_TASK) != null && hasNoListener(loggerContext)) {
if (hasNoListener(loggerContext)) {
addListener(loggerContext, location);
}
}

View File

@ -71,7 +71,12 @@ public class NacosLogbackConfiguratorAdapterV1 extends JoranConfigurator {
URLConnection urlConnection = url.openConnection();
urlConnection.setUseCaches(false);
in = urlConnection.getInputStream();
doConfigure(in, url.toExternalForm());
if (hasNewDoConfigureApi()) {
doConfigure(in, url.toExternalForm());
} else {
// adapter old version of logback below 1.1.10
doConfigure(in);
}
} catch (IOException ioe) {
String errMsg = "Could not open URL [" + url + "].";
addError(errMsg, ioe);
@ -89,4 +94,17 @@ public class NacosLogbackConfiguratorAdapterV1 extends JoranConfigurator {
}
}
/**
* Since logback 1.1.10, Add new doConfigure API with sax systemId and use this API to do configure.
*
* @return {@code true} when logback is upper 1.1.10, otherwise {@code false}
*/
private boolean hasNewDoConfigureApi() {
try {
this.getClass().getMethod("doConfigure", InputStream.class, String.class);
return true;
} catch (NoSuchMethodException e) {
return false;
}
}
}