From 79058f2a497dd8da16a7259a74364519a09bc68c Mon Sep 17 00:00:00 2001 From: Siphalor Date: Mon, 3 Nov 2025 02:37:22 +0100 Subject: [PATCH] Fix running on older Log4J2 versions --- NOTICE.txt | 7 +++++++ .../logging/impl/Log4jApiLogFactory.java | 21 ++++++++++++++----- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/NOTICE.txt b/NOTICE.txt index d681578..1a028b8 100644 --- a/NOTICE.txt +++ b/NOTICE.txt @@ -3,3 +3,10 @@ Copyright 2001-2025 The Apache Software Foundation This product includes software developed at 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. diff --git a/src/main/java/org/apache/commons/logging/impl/Log4jApiLogFactory.java b/src/main/java/org/apache/commons/logging/impl/Log4jApiLogFactory.java index 482bffe..0ce36a3 100644 --- a/src/main/java/org/apache/commons/logging/impl/Log4jApiLogFactory.java +++ b/src/main/java/org/apache/commons/logging/impl/Log4jApiLogFactory.java @@ -17,6 +17,7 @@ package org.apache.commons.logging.impl; import java.io.IOException; +import java.lang.reflect.Method; import java.util.concurrent.ConcurrentHashMap; 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.Marker; import org.apache.logging.log4j.MarkerManager; -import org.apache.logging.log4j.spi.AbstractLoggerAdapter; -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.spi.*; import org.apache.logging.log4j.util.StackLocatorUtil; /** @@ -155,10 +153,23 @@ public final class Log4jApiLogFactory extends LogFactory { } } private static final class LogAdapter extends AbstractLoggerAdapter { + 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 protected LoggerContext getContext() { - return getContext(LogManager.getFactory().isClassLoaderDependent() ? StackLocatorUtil.getCallerClass( + return getContext(isClassLoaderDependent ? StackLocatorUtil.getCallerClass( LogFactory.class) : null); }