28
28
import java .util .Deque ;
29
29
import java .util .Map ;
30
30
31
- import org .slf4j .helpers .BasicMDCAdapter ;
32
- import org .slf4j .helpers .NOPMDCAdapter ;
33
- import org .slf4j .helpers .Reporter ;
34
- import org .slf4j .helpers .Util ;
31
+ import org .slf4j .helpers .*;
35
32
import org .slf4j .spi .MDCAdapter ;
36
33
import org .slf4j .spi .SLF4JServiceProvider ;
37
34
@@ -68,7 +65,7 @@ public class MDC {
68
65
static final String NULL_MDCA_URL = "http://www.slf4j.org/codes.html#null_MDCA" ;
69
66
private static final String MDC_APAPTER_CANNOT_BE_NULL_MESSAGE = "MDCAdapter cannot be null. See also " + NULL_MDCA_URL ;
70
67
static final String NO_STATIC_MDC_BINDER_URL = "http://www.slf4j.org/codes.html#no_static_mdc_binder" ;
71
- static MDCAdapter mdcAdapter ;
68
+ static MDCAdapter MDC_ADAPTER ;
72
69
73
70
/**
74
71
* An adapter to remove the key when done.
@@ -88,17 +85,30 @@ public void close() {
88
85
private MDC () {
89
86
}
90
87
91
- static {
88
+ private static MDCAdapter getMDCAdapterGivenByProvider () {
92
89
SLF4JServiceProvider provider = LoggerFactory .getProvider ();
93
- if (provider != null ) {
90
+ if (provider != null ) {
91
+ // If you wish to change the mdc adapter, setting the MDC.MDCAdapter variable might be insufficient.
92
+ // Keep in mind that the provider *might* perform additional internal mdcAdapter assignments that
93
+ // you would also need to replicate/adapt.
94
+
94
95
// obtain and attach the MDCAdapter from the provider
95
- // If you wish to change the adapter, Setting the MDC.mdcAdapter variable might not be enough as
96
- // the provider might perform additional assignments that you would need to replicate/adapt.
97
- mdcAdapter = provider .getMDCAdapter ();
96
+
97
+ final MDCAdapter anAdapter = provider .getMDCAdapter ();
98
+ emitTemporaryMDCAdapterWarningIfNeeded (provider );
99
+ return anAdapter ;
98
100
} else {
99
101
Reporter .error ("Failed to find provider." );
100
102
Reporter .error ("Defaulting to no-operation MDCAdapter implementation." );
101
- mdcAdapter = new NOPMDCAdapter ();
103
+ return new NOPMDCAdapter ();
104
+ }
105
+ }
106
+
107
+ private static void emitTemporaryMDCAdapterWarningIfNeeded (SLF4JServiceProvider provider ) {
108
+ boolean isSubstitute = provider instanceof SubstituteServiceProvider ;
109
+ if (isSubstitute ) {
110
+ Reporter .info ("Temporary mdcAdapter given by SubstituteServiceProvider." );
111
+ Reporter .info ("This mdcAdapter will be replaced after backend initialization has completed." );
102
112
}
103
113
}
104
114
@@ -121,10 +131,10 @@ public static void put(String key, String val) throws IllegalArgumentException {
121
131
if (key == null ) {
122
132
throw new IllegalArgumentException ("key parameter cannot be null" );
123
133
}
124
- if (mdcAdapter == null ) {
134
+ if (getMDCAdapter () == null ) {
125
135
throw new IllegalStateException (MDC_APAPTER_CANNOT_BE_NULL_MESSAGE );
126
136
}
127
- mdcAdapter .put (key , val );
137
+ getMDCAdapter () .put (key , val );
128
138
}
129
139
130
140
/**
@@ -177,10 +187,10 @@ public static String get(String key) throws IllegalArgumentException {
177
187
throw new IllegalArgumentException ("key parameter cannot be null" );
178
188
}
179
189
180
- if (mdcAdapter == null ) {
190
+ if (getMDCAdapter () == null ) {
181
191
throw new IllegalStateException (MDC_APAPTER_CANNOT_BE_NULL_MESSAGE );
182
192
}
183
- return mdcAdapter .get (key );
193
+ return getMDCAdapter () .get (key );
184
194
}
185
195
186
196
/**
@@ -198,20 +208,20 @@ public static void remove(String key) throws IllegalArgumentException {
198
208
throw new IllegalArgumentException ("key parameter cannot be null" );
199
209
}
200
210
201
- if (mdcAdapter == null ) {
211
+ if (getMDCAdapter () == null ) {
202
212
throw new IllegalStateException (MDC_APAPTER_CANNOT_BE_NULL_MESSAGE );
203
213
}
204
- mdcAdapter .remove (key );
214
+ getMDCAdapter () .remove (key );
205
215
}
206
216
207
217
/**
208
218
* Clear all entries in the MDC of the underlying implementation.
209
219
*/
210
220
public static void clear () {
211
- if (mdcAdapter == null ) {
221
+ if (getMDCAdapter () == null ) {
212
222
throw new IllegalStateException (MDC_APAPTER_CANNOT_BE_NULL_MESSAGE );
213
223
}
214
- mdcAdapter .clear ();
224
+ getMDCAdapter () .clear ();
215
225
}
216
226
217
227
/**
@@ -222,10 +232,10 @@ public static void clear() {
222
232
* @since 1.5.1
223
233
*/
224
234
public static Map <String , String > getCopyOfContextMap () {
225
- if (mdcAdapter == null ) {
235
+ if (getMDCAdapter () == null ) {
226
236
throw new IllegalStateException (MDC_APAPTER_CANNOT_BE_NULL_MESSAGE );
227
237
}
228
- return mdcAdapter .getCopyOfContextMap ();
238
+ return getMDCAdapter () .getCopyOfContextMap ();
229
239
}
230
240
231
241
/**
@@ -240,23 +250,40 @@ public static Map<String, String> getCopyOfContextMap() {
240
250
* @since 1.5.1
241
251
*/
242
252
public static void setContextMap (Map <String , String > contextMap ) {
243
- if (mdcAdapter == null ) {
253
+ if (getMDCAdapter () == null ) {
244
254
throw new IllegalStateException (MDC_APAPTER_CANNOT_BE_NULL_MESSAGE );
245
255
}
246
- mdcAdapter .setContextMap (contextMap );
256
+ getMDCAdapter () .setContextMap (contextMap );
247
257
}
248
258
249
259
/**
250
260
* Returns the MDCAdapter instance currently in use.
251
- *
261
+ *
262
+ * Since 2.0.17, if the MDCAdapter instance is null, then this method set it to use
263
+ * the adapter returned by the SLF4JProvider. However, in the vast majority of cases
264
+ * the MDCAdapter will be set earlier (during initialization) by {@link LoggerFactory}.
265
+ *
252
266
* @return the MDcAdapter instance currently in use.
253
267
* @since 1.4.2
254
268
*/
255
269
public static MDCAdapter getMDCAdapter () {
256
- return mdcAdapter ;
270
+ if (MDC_ADAPTER == null ) {
271
+ MDC_ADAPTER = getMDCAdapterGivenByProvider ();
272
+ }
273
+ return MDC_ADAPTER ;
257
274
}
258
275
259
-
276
+ /**
277
+ * Set MDCAdapter instance to use.
278
+ *
279
+ * @since 2.0.17
280
+ */
281
+ static void setMDCAdapter (MDCAdapter anMDCAdapter ) {
282
+ if (anMDCAdapter == null ) {
283
+ throw new IllegalStateException (MDC_APAPTER_CANNOT_BE_NULL_MESSAGE );
284
+ }
285
+ MDC_ADAPTER = anMDCAdapter ;
286
+ }
260
287
261
288
/**
262
289
* Push a value into the deque(stack) referenced by 'key'.
@@ -266,10 +293,10 @@ public static MDCAdapter getMDCAdapter() {
266
293
* @since 2.0.0
267
294
*/
268
295
static public void pushByKey (String key , String value ) {
269
- if (mdcAdapter == null ) {
296
+ if (getMDCAdapter () == null ) {
270
297
throw new IllegalStateException (MDC_APAPTER_CANNOT_BE_NULL_MESSAGE );
271
298
}
272
- mdcAdapter .pushByKey (key , value );
299
+ getMDCAdapter () .pushByKey (key , value );
273
300
}
274
301
275
302
/**
@@ -280,10 +307,10 @@ static public void pushByKey(String key, String value) {
280
307
* @since 2.0.0
281
308
*/
282
309
static public String popByKey (String key ) {
283
- if (mdcAdapter == null ) {
310
+ if (getMDCAdapter () == null ) {
284
311
throw new IllegalStateException (MDC_APAPTER_CANNOT_BE_NULL_MESSAGE );
285
312
}
286
- return mdcAdapter .popByKey (key );
313
+ return getMDCAdapter () .popByKey (key );
287
314
}
288
315
289
316
/**
@@ -295,9 +322,9 @@ static public String popByKey(String key) {
295
322
* @since 2.0.0
296
323
*/
297
324
public Deque <String > getCopyOfDequeByKey (String key ) {
298
- if (mdcAdapter == null ) {
325
+ if (getMDCAdapter () == null ) {
299
326
throw new IllegalStateException (MDC_APAPTER_CANNOT_BE_NULL_MESSAGE );
300
327
}
301
- return mdcAdapter .getCopyOfDequeByKey (key );
328
+ return getMDCAdapter () .getCopyOfDequeByKey (key );
302
329
}
303
330
}
0 commit comments