Fix running on older Log4J2 versions
Some checks failed
Java CI / build (false, 11) (push) Has been cancelled
Java CI / build (false, 17) (push) Has been cancelled
Java CI / build (false, 21) (push) Has been cancelled
Java CI / build (false, 23) (push) Has been cancelled
Java CI / build (false, 8) (push) Has been cancelled
Java CI / build (true, 24-ea) (push) Has been cancelled
Some checks failed
Java CI / build (false, 11) (push) Has been cancelled
Java CI / build (false, 17) (push) Has been cancelled
Java CI / build (false, 21) (push) Has been cancelled
Java CI / build (false, 23) (push) Has been cancelled
Java CI / build (false, 8) (push) Has been cancelled
Java CI / build (true, 24-ea) (push) Has been cancelled
This commit is contained in:
@@ -3,3 +3,10 @@ Copyright 2001-2025 The Apache Software Foundation
|
|||||||
|
|
||||||
This product includes software developed at
|
This product includes software developed at
|
||||||
The Apache Software Foundation (https://www.apache.org/).
|
The Apache Software Foundation (https://www.apache.org/).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Changes by Siphalor for Tweed 5:
|
||||||
|
|
||||||
|
- Allow `Log4jApiLogFactory` to be used with older versions of Log4j2
|
||||||
|
by using reflection to access the `isClassLoaderDependent` method.
|
||||||
|
|||||||
@@ -17,6 +17,7 @@
|
|||||||
package org.apache.commons.logging.impl;
|
package org.apache.commons.logging.impl;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.ConcurrentMap;
|
import java.util.concurrent.ConcurrentMap;
|
||||||
|
|
||||||
@@ -26,10 +27,7 @@ import org.apache.logging.log4j.Level;
|
|||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Marker;
|
import org.apache.logging.log4j.Marker;
|
||||||
import org.apache.logging.log4j.MarkerManager;
|
import org.apache.logging.log4j.MarkerManager;
|
||||||
import org.apache.logging.log4j.spi.AbstractLoggerAdapter;
|
import org.apache.logging.log4j.spi.*;
|
||||||
import org.apache.logging.log4j.spi.ExtendedLogger;
|
|
||||||
import org.apache.logging.log4j.spi.LoggerAdapter;
|
|
||||||
import org.apache.logging.log4j.spi.LoggerContext;
|
|
||||||
import org.apache.logging.log4j.util.StackLocatorUtil;
|
import org.apache.logging.log4j.util.StackLocatorUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -155,10 +153,23 @@ public final class Log4jApiLogFactory extends LogFactory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
private static final class LogAdapter extends AbstractLoggerAdapter<Log> {
|
private static final class LogAdapter extends AbstractLoggerAdapter<Log> {
|
||||||
|
private final boolean isClassLoaderDependent;
|
||||||
|
|
||||||
|
public LogAdapter() {
|
||||||
|
boolean isClassLoaderDependent;
|
||||||
|
try {
|
||||||
|
LoggerContextFactory factory = LogManager.getFactory();
|
||||||
|
Method method = factory.getClass().getMethod("isClassLoaderDependent");
|
||||||
|
isClassLoaderDependent = (boolean) method.invoke(factory);
|
||||||
|
} catch (Exception e) {
|
||||||
|
isClassLoaderDependent = false;
|
||||||
|
}
|
||||||
|
this.isClassLoaderDependent = isClassLoaderDependent;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected LoggerContext getContext() {
|
protected LoggerContext getContext() {
|
||||||
return getContext(LogManager.getFactory().isClassLoaderDependent() ? StackLocatorUtil.getCallerClass(
|
return getContext(isClassLoaderDependent ? StackLocatorUtil.getCallerClass(
|
||||||
LogFactory.class) : null);
|
LogFactory.class) : null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user