Skip to content

Commit 8882b25

Browse files
committed
Remove reflection from log4j-to-jul and log4j-to-slf4j
By removing the reflective instantiation of `LoggerContextFactory` and `ThreadContextMap`, we make `log4j-to-jul` and `log4j-to-slf4j` more GraalVM-friendly.
1 parent 9ba858c commit 8882b25

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

log4j-to-jul/src/main/java/org/apache/logging/log4j/tojul/JULProvider.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import aQute.bnd.annotation.Resolution;
2020
import aQute.bnd.annotation.spi.ServiceProvider;
21+
import org.apache.logging.log4j.spi.LoggerContextFactory;
2122
import org.apache.logging.log4j.spi.Provider;
2223

2324
/**
@@ -27,7 +28,20 @@
2728
*/
2829
@ServiceProvider(value = Provider.class, resolution = Resolution.OPTIONAL)
2930
public class JULProvider extends Provider {
31+
private static final LoggerContextFactory CONTEXT_FACTORY = new JULLoggerContextFactory();
32+
3033
public JULProvider() {
31-
super(20, "2.6.0", JULLoggerContextFactory.class, null);
34+
super(20, CURRENT_VERSION);
35+
}
36+
37+
@Override
38+
public LoggerContextFactory getLoggerContextFactory() {
39+
return CONTEXT_FACTORY;
40+
}
41+
42+
@Override
43+
public String getThreadContextMap() {
44+
// JUL does not provide an MDC implementation
45+
return NO_OP_CONTEXT_MAP;
3246
}
3347
}

log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/SLF4JProvider.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,30 @@
1818

1919
import aQute.bnd.annotation.Resolution;
2020
import aQute.bnd.annotation.spi.ServiceProvider;
21+
import org.apache.logging.log4j.spi.LoggerContextFactory;
2122
import org.apache.logging.log4j.spi.Provider;
23+
import org.apache.logging.log4j.spi.ThreadContextMap;
2224

2325
/**
2426
* Bind the Log4j API to SLF4J.
2527
*/
2628
@ServiceProvider(value = Provider.class, resolution = Resolution.OPTIONAL)
2729
public class SLF4JProvider extends Provider {
30+
31+
private static final LoggerContextFactory CONTEXT_FACTORY = new SLF4JLoggerContextFactory();
32+
private static final ThreadContextMap THREAD_CONTEXT_MAP = new MDCContextMap();
33+
2834
public SLF4JProvider() {
29-
super(15, "2.6.0", SLF4JLoggerContextFactory.class, MDCContextMap.class);
35+
super(15, CURRENT_VERSION);
36+
}
37+
38+
@Override
39+
public LoggerContextFactory getLoggerContextFactory() {
40+
return CONTEXT_FACTORY;
41+
}
42+
43+
@Override
44+
public ThreadContextMap getThreadContextMapInstance() {
45+
return THREAD_CONTEXT_MAP;
3046
}
3147
}

0 commit comments

Comments
 (0)