Skip to content

Commit

Permalink
[ISSUE#11956] Fix logging module old bugs (#12011)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
KomachiSion authored Apr 24, 2024
1 parent de10c07 commit da3e88b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 22 deletions.
18 changes: 0 additions & 18 deletions client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
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;
Expand Down Expand Up @@ -91,7 +90,7 @@ public void loadConfiguration(NacosLoggingProperties loggingProperties) {
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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ public void configure(URL url) throws Exception {
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);
Expand All @@ -89,4 +94,17 @@ public void configure(URL url) throws Exception {
}
}

/**
* 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;
}
}
}

0 comments on commit da3e88b

Please sign in to comment.