From 6d844ffbb6cda4c34695187037495f7d45bb8614 Mon Sep 17 00:00:00 2001 From: games647 Date: Thu, 12 Jan 2023 14:31:33 +0100 Subject: [PATCH] Forward SLF4J log messages to plugin logger Related #172 --- pom.xml | 21 +++----- .../games647/lagmonitor/LagMonitor.java | 3 ++ .../lagmonitor/logging/ForwardLogService.java | 39 +++++++++++++++ .../logging/ForwardingLoggerFactory.java | 48 +++++++++++++++++++ .../org.slf4j.spi.SLF4JServiceProvider | 1 + 5 files changed, 98 insertions(+), 14 deletions(-) create mode 100644 src/main/java/com/github/games647/lagmonitor/logging/ForwardLogService.java create mode 100644 src/main/java/com/github/games647/lagmonitor/logging/ForwardingLoggerFactory.java create mode 100644 src/main/resources/META-INF/services/org.slf4j.spi.SLF4JServiceProvider diff --git a/pom.xml b/pom.xml index 0b2dbb4..04225fb 100644 --- a/pom.xml +++ b/pom.xml @@ -62,10 +62,6 @@ oshi.vmmacaddr.properties - - org.slf4j - lagmonitor.slf4j - @@ -140,16 +136,6 @@ paper-api 1.19-R0.1-SNAPSHOT provided - - - org.slf4j - * - - - net.md-5 - * - - @@ -179,6 +165,13 @@ + + + org.slf4j + slf4j-jdk14 + 2.0.6 + + com.github.oshi diff --git a/src/main/java/com/github/games647/lagmonitor/LagMonitor.java b/src/main/java/com/github/games647/lagmonitor/LagMonitor.java index e5a54f1..9201e9d 100644 --- a/src/main/java/com/github/games647/lagmonitor/LagMonitor.java +++ b/src/main/java/com/github/games647/lagmonitor/LagMonitor.java @@ -23,6 +23,7 @@ import com.github.games647.lagmonitor.listener.GraphListener; import com.github.games647.lagmonitor.listener.PageManager; import com.github.games647.lagmonitor.listener.ThreadSafetyListener; +import com.github.games647.lagmonitor.logging.ForwardingLoggerFactory; import com.github.games647.lagmonitor.storage.MonitorSaveTask; import com.github.games647.lagmonitor.storage.NativeSaveTask; import com.github.games647.lagmonitor.storage.Storage; @@ -68,6 +69,8 @@ public class LagMonitor extends JavaPlugin { @Override public void onLoad() { + ForwardingLoggerFactory.PARENT_LOGGER = getLogger(); + nativeData.setupNativeAdapter(); } diff --git a/src/main/java/com/github/games647/lagmonitor/logging/ForwardLogService.java b/src/main/java/com/github/games647/lagmonitor/logging/ForwardLogService.java new file mode 100644 index 0000000..61b8050 --- /dev/null +++ b/src/main/java/com/github/games647/lagmonitor/logging/ForwardLogService.java @@ -0,0 +1,39 @@ +package com.github.games647.lagmonitor.logging; + +import org.slf4j.ILoggerFactory; +import org.slf4j.IMarkerFactory; +import org.slf4j.jul.JULServiceProvider; +import org.slf4j.spi.MDCAdapter; +import org.slf4j.spi.SLF4JServiceProvider; + +public class ForwardLogService implements SLF4JServiceProvider { + + private ILoggerFactory loggerFactory; + private JULServiceProvider delegate; + + public ILoggerFactory getLoggerFactory() { + return this.loggerFactory; + } + + public IMarkerFactory getMarkerFactory() { + return delegate.getMarkerFactory(); + } + + public MDCAdapter getMDCAdapter() { + return delegate.getMDCAdapter(); + } + + @Override + public String getRequesteApiVersion() { + return delegate.getRequestedApiVersion(); + } + + public String getRequestedApiVersion() { + return delegate.getRequestedApiVersion(); + } + + public void initialize() { + this.delegate = new JULServiceProvider(); + this.loggerFactory = new ForwardingLoggerFactory(); + } +} diff --git a/src/main/java/com/github/games647/lagmonitor/logging/ForwardingLoggerFactory.java b/src/main/java/com/github/games647/lagmonitor/logging/ForwardingLoggerFactory.java new file mode 100644 index 0000000..f05d26a --- /dev/null +++ b/src/main/java/com/github/games647/lagmonitor/logging/ForwardingLoggerFactory.java @@ -0,0 +1,48 @@ +package com.github.games647.lagmonitor.logging; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +import org.slf4j.ILoggerFactory; +import org.slf4j.Logger; +import org.slf4j.jul.JDK14LoggerAdapter; + +public class ForwardingLoggerFactory implements ILoggerFactory { + + private final ConcurrentMap loggerMap = new ConcurrentHashMap<>(); + + public static java.util.logging.Logger PARENT_LOGGER; + + @Override + public Logger getLogger(String name) { + return loggerMap.computeIfAbsent(name, key -> { + java.util.logging.Logger julLogger; + if (PARENT_LOGGER == null) { + julLogger = java.util.logging.Logger.getLogger(name); + } else { + julLogger = PARENT_LOGGER; + } + + Logger newInstance = null; + try { + newInstance = createJDKLogger(julLogger); + } catch (NoSuchMethodException | InvocationTargetException | + InstantiationException | IllegalAccessException e) { + e.printStackTrace(); + System.out.println("Failed to created logging instance"); + } + + return newInstance; + }); + } + + protected static Logger createJDKLogger(java.util.logging.Logger parent) + throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException { + Class adapterClass = JDK14LoggerAdapter.class; + Constructor cons = adapterClass.getDeclaredConstructor(java.util.logging.Logger.class); + cons.setAccessible(true); + return (Logger) cons.newInstance(parent); + } +} diff --git a/src/main/resources/META-INF/services/org.slf4j.spi.SLF4JServiceProvider b/src/main/resources/META-INF/services/org.slf4j.spi.SLF4JServiceProvider new file mode 100644 index 0000000..1149156 --- /dev/null +++ b/src/main/resources/META-INF/services/org.slf4j.spi.SLF4JServiceProvider @@ -0,0 +1 @@ +com.github.games647.lagmonitor.logging.ForwardLogService