-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Forward SLF4J log messages to plugin logger
Related #172
- Loading branch information
Showing
5 changed files
with
98 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/java/com/github/games647/lagmonitor/logging/ForwardLogService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/com/github/games647/lagmonitor/logging/ForwardingLoggerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
src/main/resources/META-INF/services/org.slf4j.spi.SLF4JServiceProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
com.github.games647.lagmonitor.logging.ForwardLogService |