Skip to content

Commit 40b3e51

Browse files
committed
In SLF4JProviders, move initialization of the markerFactory and
mdcAdapter fields to the constructor. Added comments regarding LoggerFactory expectation of providers to initialize their MDCAdapter and markerFactory field as early as possible, preferably at construction time. Signed-off-by: Ceki Gulcu <ceki@qos.ch>
1 parent 9bd0374 commit 40b3e51

File tree

6 files changed

+55
-14
lines changed

6 files changed

+55
-14
lines changed

slf4j-api/src/main/java/org/slf4j/LoggerFactory.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,8 @@ private final static void bind() {
220220
/**
221221
* The value of PROVIDER.getMDCAdapter() can be null while PROVIDER has not yet initialized.
222222
*
223-
* However,
224-
*
223+
* However, SLF4JServiceProvider implementations are expected to initialize their internal
224+
* MDCAdapter field in their constructor or on field declaration.
225225
*/
226226
private static void earlyBindMDCAdapter() {
227227
MDCAdapter mdcAdapter = PROVIDER.getMDCAdapter();

slf4j-api/src/main/java/org/slf4j/helpers/SubstituteServiceProvider.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,18 @@
77

88
public class SubstituteServiceProvider implements SLF4JServiceProvider {
99
private final SubstituteLoggerFactory loggerFactory = new SubstituteLoggerFactory();
10-
private final IMarkerFactory markerFactory = new BasicMarkerFactory();
11-
private final MDCAdapter mdcAdapter = new BasicMDCAdapter();
10+
11+
// LoggerFactory expects providers to initialize markerFactory as early as possible.
12+
private final IMarkerFactory markerFactory;
13+
14+
// LoggerFactory expects providers to initialize their MDCAdapter field
15+
// as early as possible, preferably at construction time.
16+
private final MDCAdapter mdcAdapter;
17+
18+
public SubstituteServiceProvider() {
19+
markerFactory = new BasicMarkerFactory();
20+
mdcAdapter = new BasicMDCAdapter();
21+
}
1222

1323
@Override
1424
public ILoggerFactory getLoggerFactory() {
@@ -36,6 +46,5 @@ public String getRequestedApiVersion() {
3646

3747
@Override
3848
public void initialize() {
39-
4049
}
4150
}

slf4j-jdk14/src/main/java/org/slf4j/jul/JULServiceProvider.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,16 @@ public class JULServiceProvider implements SLF4JServiceProvider {
1717
public static String REQUESTED_API_VERSION = "2.0.99"; // !final
1818

1919
private ILoggerFactory loggerFactory;
20-
private IMarkerFactory markerFactory = new BasicMarkerFactory();
21-
private MDCAdapter mdcAdapter = new BasicMDCAdapter();
20+
// LoggerFactory expects providers to initialize markerFactory as early as possible.
21+
private final IMarkerFactory markerFactory;
22+
// LoggerFactory expects providers to initialize their MDCAdapter field
23+
// as early as possible, preferably at construction time.
24+
private final MDCAdapter mdcAdapter;
25+
26+
public JULServiceProvider() {
27+
markerFactory = new BasicMarkerFactory();
28+
mdcAdapter = new BasicMDCAdapter();
29+
}
2230

2331
@Override
2432
public ILoggerFactory getLoggerFactory() {

slf4j-nop/src/main/java/org/slf4j/nop/NOPServiceProvider.java

+11-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,18 @@ public class NOPServiceProvider implements SLF4JServiceProvider {
2323
public static String REQUESTED_API_VERSION = "2.0.99"; // !final
2424

2525
private final ILoggerFactory loggerFactory = new NOPLoggerFactory();
26-
private final IMarkerFactory markerFactory = new BasicMarkerFactory();
27-
private final MDCAdapter mdcAdapter = new NOPMDCAdapter();
2826

27+
// LoggerFactory expects providers to initialize markerFactory as early as possible.
28+
private final IMarkerFactory markerFactory;
29+
30+
// LoggerFactory expects providers to initialize their MDCAdapter field
31+
// as early as possible, preferably at construction time.
32+
private final MDCAdapter mdcAdapter;
33+
34+
public NOPServiceProvider() {
35+
markerFactory = new BasicMarkerFactory();
36+
mdcAdapter = new NOPMDCAdapter();
37+
}
2938
public ILoggerFactory getLoggerFactory() {
3039
return loggerFactory;
3140
}
@@ -44,7 +53,6 @@ public String getRequestedApiVersion() {
4453
}
4554

4655
public void initialize() {
47-
4856
}
4957

5058

slf4j-reload4j/src/main/java/org/slf4j/reload4j/Reload4jServiceProvider.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,17 @@ public class Reload4jServiceProvider implements SLF4JServiceProvider {
1919
public static String REQUESTED_API_VERSION = "2.0.99"; // !final
2020

2121
private ILoggerFactory loggerFactory;
22-
private IMarkerFactory markerFactory = new BasicMarkerFactory();
23-
private MDCAdapter mdcAdapter = new Reload4jMDCAdapter();
22+
23+
// LoggerFactory expects providers to initialize markerFactory as early as possible.
24+
private final IMarkerFactory markerFactory;
25+
26+
// LoggerFactory expects providers to have a valid MDCAdapter field
27+
// as early as possible, preferably at construction time.
28+
private final MDCAdapter mdcAdapter;
2429

2530
public Reload4jServiceProvider() {
31+
markerFactory = new BasicMarkerFactory();
32+
mdcAdapter = new Reload4jMDCAdapter();
2633
try {
2734
@SuppressWarnings("unused")
2835
Level level = Level.TRACE;

slf4j-simple/src/main/java/org/slf4j/simple/SimpleServiceProvider.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,16 @@ public class SimpleServiceProvider implements SLF4JServiceProvider {
1717
public static String REQUESTED_API_VERSION = "2.0.99"; // !final
1818

1919
private ILoggerFactory loggerFactory;
20-
private IMarkerFactory = new BasicMarkerFactory();
21-
private MDCAdapter mdcAdapter = new NOPMDCAdapter();
20+
// LoggerFactory expects providers to initialize markerFactory as early as possible.
21+
private final IMarkerFactory markerFactory;
22+
// LoggerFactory expects providers to initialize their MDCAdapter field
23+
// as early as possible, preferably at construction time.
24+
private final MDCAdapter mdcAdapter;
25+
26+
public SimpleServiceProvider() {
27+
markerFactory = new BasicMarkerFactory();
28+
mdcAdapter = new NOPMDCAdapter();
29+
}
2230

2331
public ILoggerFactory getLoggerFactory() {
2432
return loggerFactory;
@@ -42,6 +50,7 @@ public String getRequestedApiVersion() {
4250
@Override
4351
public void initialize() {
4452
loggerFactory = new SimpleLoggerFactory();
53+
4554
}
4655

4756
}

0 commit comments

Comments
 (0)