Skip to content

Commit

Permalink
Forward SLF4J log messages to plugin logger
Browse files Browse the repository at this point in the history
Related #172
  • Loading branch information
games647 committed Jan 12, 2023
1 parent bb40de5 commit 6d844ff
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 14 deletions.
21 changes: 7 additions & 14 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@
<exclude>oshi.vmmacaddr.properties</exclude>
</excludes>
</relocation>
<relocation>
<pattern>org.slf4j</pattern>
<shadedPattern>lagmonitor.slf4j</shadedPattern>
</relocation>
</relocations>
<artifactSet>
<excludes>
Expand Down Expand Up @@ -140,16 +136,6 @@
<artifactId>paper-api</artifactId>
<version>1.19-R0.1-SNAPSHOT</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>*</artifactId>
</exclusion>
<exclusion>
<groupId>net.md-5</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>

<!--Server API for Spigot-->
Expand Down Expand Up @@ -179,6 +165,13 @@
</exclusions>
</dependency>

<!-- JDK logging bridge to forward logging messages to the plugin logger -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>2.0.6</version>
</dependency>

<!-- Include core explicitly in order to ship the default configuration files in it -->
<dependency>
<groupId>com.github.oshi</groupId>
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/github/games647/lagmonitor/LagMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,6 +69,8 @@ public class LagMonitor extends JavaPlugin {

@Override
public void onLoad() {
ForwardingLoggerFactory.PARENT_LOGGER = getLogger();

nativeData.setupNativeAdapter();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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<String, Logger> 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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.github.games647.lagmonitor.logging.ForwardLogService

0 comments on commit 6d844ff

Please sign in to comment.