Skip to content

Commit

Permalink
fix: Fix ArcLoggerFactory not taking into account common logger wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
phinner committed Dec 18, 2023
1 parent edb68ba commit 9185a97
Showing 1 changed file with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@

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;
import mindustry.mod.Plugin;
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 {

Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 9185a97

Please sign in to comment.