Skip to content

Commit

Permalink
Support logback 1.4.9, fix logback compatibility (#188)
Browse files Browse the repository at this point in the history
* update 2.0.1
add some legacy api

* update ci

* fix logback compatibility

---------

Co-authored-by: “HzjNeverStop” <“441627022@qq.com”>
  • Loading branch information
HzjNeverStop and “HzjNeverStop” authored Aug 11, 2023
1 parent bccc79d commit ec43d9b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>17</java.version>
<logback.version>1.4.5</logback.version>
<slf4j.version>2.0.6</slf4j.version>
<logback.version>1.4.9</logback.version>
<slf4j.version>2.0.7</slf4j.version>
<log4j2.version>2.19.0</log4j2.version>
<commons.io.version>2.7</commons.io.version>
<commons.logging.version>1.1.1</commons.logging.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.filter.ThresholdFilter;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.turbo.TurboFilter;
import ch.qos.logback.classic.util.ContextInitializer;
import ch.qos.logback.core.ConsoleAppender;
import ch.qos.logback.core.LogbackException;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.spi.FilterReply;
import ch.qos.logback.core.spi.ScanException;
Expand All @@ -34,8 +36,11 @@
import com.alipay.sofa.common.space.SpaceId;
import com.alipay.sofa.common.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.MDC;
import org.slf4j.Marker;
import org.slf4j.spi.MDCAdapter;

import java.lang.reflect.Method;
import java.net.URL;
import java.util.Map;
import java.util.Properties;
Expand All @@ -46,11 +51,26 @@
*/
public class LogbackLoggerSpaceFactory extends AbstractLoggerSpaceFactory {

private static final Method logContextSetMDCAdapterMethod;

private final SpaceId spaceId;
private final LoggerContext loggerContext;
private final Properties properties;
private ConsoleAppender<ILoggingEvent> consoleAppender;

static {
// Resolve logContext#setMDCAdapter method if logback version >= 1.4.8
Method logContextSetMDCAdapter;
try {
logContextSetMDCAdapter = LoggerContext.class.getDeclaredMethod("setMDCAdapter",
MDCAdapter.class);
} catch (Throwable t) {
logContextSetMDCAdapter = null;
}

logContextSetMDCAdapterMethod = logContextSetMDCAdapter;
}

public LogbackLoggerSpaceFactory(SpaceId spaceId, LoggerContext loggerContext,
Properties properties, URL confFile, String source) {
super(source);
Expand All @@ -62,11 +82,21 @@ public LogbackLoggerSpaceFactory(SpaceId spaceId, LoggerContext loggerContext,
loggerContext.putProperty((String) entry.getKey(), (String) entry.getValue());
}
try {
new ContextInitializer(loggerContext).configureByResource(confFile);
new ContextInitializer(loggerContext);
configureByResource(confFile, loggerContext);
} catch (JoranException e) {
throw new IllegalStateException("Logback loggerSpaceFactory build error", e);
}

// invoke loggerContext.setMDCAdapter(MDC.getMDCAdapter());
if (logContextSetMDCAdapterMethod != null) {
try {
logContextSetMDCAdapterMethod.invoke(loggerContext, MDC.getMDCAdapter());
} catch (Throwable t) {
throw new IllegalStateException("Failed to invoke setMDCAdapter method", t);
}
}

String value = properties.getProperty(String.format(
Constants.SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_SWITCH, spaceId.getSpaceName()));
if (StringUtil.isEmpty(value)) {
Expand Down Expand Up @@ -161,4 +191,21 @@ private Level toLogbackLevel(AdapterLevel adapterLevel) {
+ " is unknown when adapter to logback.");
};
}

// logback 1.4.8 remove this method: https://github.com/qos-ch/logback/commit/4b06e062488e4cb87f22be6ae96e4d7d6350ed6b
public static void configureByResource(URL url, LoggerContext loggerContext)
throws JoranException {
if (url == null) {
throw new IllegalArgumentException("URL argument cannot be null");
}
final String urlString = url.toString();
if (urlString.endsWith("xml")) {
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(loggerContext);
configurator.doConfigure(url);
} else {
throw new LogbackException("Unexpected filename extension of file [" + url
+ "]. Should be .xml");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.util.ContextInitializer;
import ch.qos.logback.core.joran.spi.JoranException;
import com.alipay.sofa.common.log.factory.LogbackLoggerSpaceFactory;
import org.junit.Assert;
import org.junit.Test;

Expand All @@ -35,7 +36,8 @@ public void testIndependentSpaceLogback() throws JoranException {

URL url1 = LogbackTest.class.getResource("/com/alipay/sofa/rpc/log/logback/log-conf.xml");
LoggerContext loggerContext1 = new LoggerContext();
new ContextInitializer(loggerContext1).configureByResource(url1);
new ContextInitializer(loggerContext1);
LogbackLoggerSpaceFactory.configureByResource(url1, loggerContext1);
ch.qos.logback.classic.Logger logger1 = loggerContext1.getLogger("com.foo.Bar");
logger1.info("log4j2 - 1");
Assert.assertNotNull(logger1);
Expand All @@ -44,7 +46,8 @@ public void testIndependentSpaceLogback() throws JoranException {

URL url2 = LogbackTest.class.getResource("/com/alipay/sofa/rpc/log/logback/logback_b.xml");
LoggerContext loggerContext2 = new LoggerContext();
new ContextInitializer(loggerContext2).configureByResource(url2);
new ContextInitializer(loggerContext2);
LogbackLoggerSpaceFactory.configureByResource(url2, loggerContext2);
Logger logger2 = loggerContext2.getLogger("com.foo.Bar2");
logger2.info("log4j2 - 222");
Assert.assertNotNull(logger2);
Expand Down

0 comments on commit ec43d9b

Please sign in to comment.