From 9185a97247ac3a79d5edca82c8d86253b00ffaec Mon Sep 17 00:00:00 2001 From: phinner <62483793+phinner@users.noreply.github.com> Date: Mon, 18 Dec 2023 03:29:19 +0100 Subject: [PATCH] fix: Fix ArcLoggerFactory not taking into account common logger wrappers --- .../core/logging/ArcLoggerFactory.java | 31 +++++++++++++------ 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/distributor-core/src/main/java/fr/xpdustry/distributor/core/logging/ArcLoggerFactory.java b/distributor-core/src/main/java/fr/xpdustry/distributor/core/logging/ArcLoggerFactory.java index bce6b9e9..78356d7d 100644 --- a/distributor-core/src/main/java/fr/xpdustry/distributor/core/logging/ArcLoggerFactory.java +++ b/distributor-core/src/main/java/fr/xpdustry/distributor/core/logging/ArcLoggerFactory.java @@ -20,6 +20,7 @@ import fr.xpdustry.distributor.api.plugin.PluginDescriptor; import java.io.IOException; +import java.util.Arrays; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import mindustry.mod.ModClassLoader; @@ -27,7 +28,6 @@ import org.checkerframework.checker.nullness.qual.Nullable; import org.slf4j.ILoggerFactory; import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public final class ArcLoggerFactory implements ILoggerFactory { @@ -49,15 +49,14 @@ public Logger getLogger(final String name) { try { caller = Class.forName(name); } catch (final ClassNotFoundException ignored1) { - final var stacktrace = Thread.currentThread().getStackTrace(); - if (stacktrace.length >= 4 && stacktrace[2].getClassName().equals(LoggerFactory.class.getName())) { - try { - caller = Class.forName(stacktrace[3].getClassName()); - cache = false; - } catch (final ClassNotFoundException ignored2) { - return new ArcLogger(name, null); - } - } else { + final var candidate = tryFindCaller(Thread.currentThread().getStackTrace()); + if (candidate == null) { + return new ArcLogger(name, null); + } + try { + caller = Class.forName(candidate); + cache = false; + } catch (final ClassNotFoundException ignored2) { return new ArcLogger(name, null); } } @@ -100,4 +99,16 @@ private ArcLogger createLogger(final String name, final @Nullable String plugin, } return logger; } + + private @Nullable String tryFindCaller(final StackTraceElement[] stacktrace) { + return Arrays.stream(stacktrace) + .skip(3) // 0: stacktrace call, 1: ArcLoggerFactory#getLogger, 2: LoggerFactory#getLogger + .map(StackTraceElement::getClassName) + // Skips the logger wrappers + .dropWhile(clazz -> clazz.startsWith("org.slf4j") + || clazz.startsWith("java.util.logging") + || clazz.startsWith("sun.util.logging")) + .findFirst() + .orElse(null); + } }