1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.logging.log4j;
19
20 import java.io.Serializable;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Map;
27
28 import org.apache.logging.log4j.message.ParameterizedMessage;
29 import org.apache.logging.log4j.spi.DefaultThreadContextMap;
30 import org.apache.logging.log4j.spi.DefaultThreadContextStack;
31 import org.apache.logging.log4j.spi.LoggerContextFactory;
32 import org.apache.logging.log4j.spi.MutableThreadContextStack;
33 import org.apache.logging.log4j.spi.Provider;
34 import org.apache.logging.log4j.spi.ThreadContextMap;
35 import org.apache.logging.log4j.spi.ThreadContextStack;
36 import org.apache.logging.log4j.status.StatusLogger;
37 import org.apache.logging.log4j.util.PropertiesUtil;
38 import org.apache.logging.log4j.util.ProviderUtil;
39
40
41
42
43
44
45
46
47 public final class ThreadContext {
48
49
50
51
52 public static final Map<String, String> EMPTY_MAP = Collections.emptyMap();
53
54
55
56
57 public static final ThreadContextStack EMPTY_STACK = new MutableThreadContextStack(new ArrayList<String>());
58
59 private static final String DISABLE_MAP = "disableThreadContextMap";
60 private static final String DISABLE_STACK = "disableThreadContextStack";
61 private static final String DISABLE_ALL = "disableThreadContext";
62 private static final String THREAD_CONTEXT_KEY = "log4j2.threadContextMap";
63
64 private static boolean all;
65 private static boolean useMap;
66 private static boolean useStack;
67 private static ThreadContextMap contextMap;
68 private static ThreadContextStack contextStack;
69 private static final Logger LOGGER = StatusLogger.getLogger();
70
71 static {
72 final PropertiesUtil managerProps = PropertiesUtil.getProperties();
73 all = managerProps.getBooleanProperty(DISABLE_ALL);
74 useStack = !(managerProps.getBooleanProperty(DISABLE_STACK) || all);
75 contextStack = new DefaultThreadContextStack(useStack);
76
77 useMap = !(managerProps.getBooleanProperty(DISABLE_MAP) || all);
78 String threadContextMapName = managerProps.getStringProperty(THREAD_CONTEXT_KEY);
79 final ClassLoader cl = ProviderUtil.findClassLoader();
80 if (threadContextMapName != null) {
81 try {
82 final Class<?> clazz = cl.loadClass(threadContextMapName);
83 if (ThreadContextMap.class.isAssignableFrom(clazz)) {
84 contextMap = (ThreadContextMap) clazz.newInstance();
85 }
86 } catch (final ClassNotFoundException cnfe) {
87 LOGGER.error("Unable to locate configured LoggerContextFactory {}", threadContextMapName);
88 } catch (final Exception ex) {
89 LOGGER.error("Unable to create configured LoggerContextFactory {}", threadContextMapName, ex);
90 }
91 }
92 if (contextMap == null && ProviderUtil.hasProviders()) {
93 final LoggerContextFactory factory = LogManager.getFactory();
94 final Iterator<Provider> providers = ProviderUtil.getProviders();
95 while (providers.hasNext()) {
96 final Provider provider = providers.next();
97 threadContextMapName = provider.getThreadContextMap();
98 final String factoryClassName = provider.getClassName();
99 if (threadContextMapName != null && factory.getClass().getName().equals(factoryClassName)) {
100 try {
101 final Class<?> clazz = cl.loadClass(threadContextMapName);
102 if (ThreadContextMap.class.isAssignableFrom(clazz)) {
103 contextMap = (ThreadContextMap) clazz.newInstance();
104 break;
105 }
106 } catch (final ClassNotFoundException cnfe) {
107 LOGGER.error("Unable to locate configured LoggerContextFactory {}", threadContextMapName);
108 contextMap = new DefaultThreadContextMap(useMap);
109 } catch (final Exception ex) {
110 LOGGER.error("Unable to create configured LoggerContextFactory {}", threadContextMapName, ex);
111 contextMap = new DefaultThreadContextMap(useMap);
112 }
113 }
114 }
115 }
116 if (contextMap == null) {
117 contextMap = new DefaultThreadContextMap(useMap);
118 }
119 }
120
121 private ThreadContext() {
122
123 }
124
125
126
127
128
129
130
131
132
133
134
135 public static void put(final String key, final String value) {
136 contextMap.put(key, value);
137 }
138
139
140
141
142
143
144
145
146 public static String get(final String key) {
147 return contextMap.get(key);
148 }
149
150
151
152
153
154 public static void remove(final String key) {
155 contextMap.remove(key);
156 }
157
158
159
160
161 public static void clear() {
162 contextMap.clear();
163 }
164
165
166
167
168
169
170 public static boolean containsKey(final String key) {
171 return contextMap.containsKey(key);
172 }
173
174
175
176
177
178 public static Map<String, String> getContext() {
179 return contextMap.getCopy();
180 }
181
182
183
184
185
186 public static Map<String, String> getImmutableContext() {
187 final Map<String, String> map = contextMap.getImmutableMapOrNull();
188 return map == null ? EMPTY_MAP : map;
189 }
190
191
192
193
194
195 public static boolean isEmpty() {
196 return contextMap.isEmpty();
197 }
198
199
200
201
202 public static void clearStack() {
203 contextStack.clear();
204 }
205
206
207
208
209
210 public static ContextStack cloneStack() {
211 return contextStack.copy();
212 }
213
214
215
216
217
218 public static ContextStack getImmutableStack() {
219 return contextStack;
220 }
221
222
223
224
225
226 public static void setStack(final Collection<String> stack) {
227 if (stack.size() == 0 || !useStack) {
228 return;
229 }
230 contextStack.clear();
231 contextStack.addAll(stack);
232 }
233
234
235
236
237
238
239
240 public static int getDepth() {
241 return contextStack.getDepth();
242 }
243
244
245
246
247
248
249
250
251
252 public static String pop() {
253 return contextStack.pop();
254 }
255
256
257
258
259
260
261
262
263
264
265 public static String peek() {
266 return contextStack.peek();
267 }
268
269
270
271
272
273
274
275
276
277 public static void push(final String message) {
278 contextStack.push(message);
279 }
280
281
282
283
284
285
286
287
288
289
290
291 public static void push(final String message, final Object... args) {
292 contextStack.push(ParameterizedMessage.format(message, args));
293 }
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313 public static void removeStack() {
314 contextStack.clear();
315 }
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346 public static void trim(final int depth) {
347 contextStack.trim(depth);
348 }
349
350
351
352
353 public interface ContextStack extends Serializable {
354
355
356
357
358 void clear();
359
360
361
362
363
364
365 String pop();
366
367
368
369
370
371 String peek();
372
373
374
375
376
377 void push(String message);
378
379
380
381
382
383 int getDepth();
384
385
386
387
388
389 List<String> asList();
390
391
392
393
394
395 void trim(int depth);
396
397
398
399
400
401 ContextStack copy();
402 }
403 }