Skip to content

Commit 0547fa0

Browse files
committed
Remove the Scopedcontext.Renderable interface
1 parent 2f47c11 commit 0547fa0

File tree

4 files changed

+48
-104
lines changed

4 files changed

+48
-104
lines changed

log4j-api-test/src/main/java/org/apache/logging/log4j/test/TestLogger.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ protected void log(
8181
sb.append(' ');
8282
}
8383
sb.append(message.getFormattedMessage());
84-
Map<String, ScopedContext.Renderable> contextMap = ScopedContext.getContextMap();
84+
Map<String, Object> contextMap = ScopedContext.getContextMap();
8585
final Map<String, String> mdc = new HashMap<>(ThreadContext.getImmutableContext());
8686
if (contextMap != null && !contextMap.isEmpty()) {
87-
contextMap.forEach((key, value) -> mdc.put(key, value.render()));
87+
contextMap.forEach((key, value) -> mdc.put(key, value.toString()));
8888
}
8989
if (!mdc.isEmpty()) {
9090
sb.append(' ');

log4j-api/src/main/java/org/apache/logging/log4j/ScopedContext.java

+43-99
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class ScopedContext {
5656
private static final ThreadLocal<Deque<Instance>> scopedContext = new ThreadLocal<>();
5757

5858
/**
59-
* Returns an immutable Map containing all the key/value pairs as Renderable objects.
59+
* Returns an immutable Map containing all the key/value pairs as Object objects.
6060
* @return An immutable copy of the Map at the current scope.
6161
*/
6262
private static Optional<Instance> getContext() {
@@ -99,9 +99,9 @@ private static void removeScopedContext() {
9999
* @hidden
100100
* Returns an unmodifiable copy of the current ScopedContext Map. This method should
101101
* only be used by implementations of Log4j API.
102-
* @return the Map of Renderable objects.
102+
* @return the Map of Object objects.
103103
*/
104-
public static Map<String, Renderable> getContextMap() {
104+
public static Map<String, Object> getContextMap() {
105105
Optional<Instance> context = getContext();
106106
if (context.isPresent()
107107
&& context.get().contextMap != null
@@ -129,13 +129,7 @@ public static int size() {
129129
@SuppressWarnings("unchecked")
130130
public static <T> T get(String key) {
131131
Optional<Instance> context = getContext();
132-
if (context.isPresent()) {
133-
Renderable renderable = context.get().contextMap.get(key);
134-
if (renderable != null) {
135-
return (T) renderable.getObject();
136-
}
137-
}
138-
return null;
132+
return context.map(instance -> (T) instance.contextMap.get(key)).orElse(null);
139133
}
140134

141135
/**
@@ -146,9 +140,9 @@ public static <T> T get(String key) {
146140
public static String getString(String key) {
147141
Optional<Instance> context = getContext();
148142
if (context.isPresent()) {
149-
Renderable renderable = context.get().contextMap.get(key);
150-
if (renderable != null) {
151-
return renderable.render();
143+
Object obj = context.get().contextMap.get(key);
144+
if (obj != null) {
145+
return obj.toString();
152146
}
153147
}
154148
return null;
@@ -161,9 +155,9 @@ public static String getString(String key) {
161155
public static void addAll(Map<String, String> map) {
162156
Optional<Instance> context = getContext();
163157
if (context.isPresent()) {
164-
Map<String, Renderable> contextMap = context.get().contextMap;
158+
Map<String, Object> contextMap = context.get().contextMap;
165159
if (contextMap != null && !contextMap.isEmpty()) {
166-
contextMap.forEach((key, value) -> map.put(key, value.render()));
160+
contextMap.forEach((key, value) -> map.put(key, value.toString()));
167161
}
168162
}
169163
}
@@ -178,12 +172,11 @@ public static void addAll(Map<String, String> map) {
178172
*/
179173
public static Instance where(String key, Object value) {
180174
if (value != null) {
181-
Renderable renderable = value instanceof Renderable ? (Renderable) value : new ObjectRenderable(value);
182175
Instance parent = getContext().isPresent() ? getContext().get() : null;
183-
return new Instance(parent, key, renderable);
176+
return new Instance(parent, key, value);
184177
} else {
185178
if (getContext().isPresent()) {
186-
Map<String, Renderable> map = getContextMap();
179+
Map<String, Object> map = getContextMap();
187180
map.remove(key);
188181
return new Instance(map);
189182
}
@@ -209,19 +202,18 @@ public static Instance where(String key, Supplier<Object> supplier) {
209202
*/
210203
public static Instance where(Map<String, ?> map) {
211204
if (map != null && !map.isEmpty()) {
212-
Map<String, Renderable> renderableMap = new HashMap<>();
205+
Map<String, Object> objectMap = new HashMap<>();
213206
if (getContext().isPresent()) {
214-
renderableMap.putAll(getContext().get().contextMap);
207+
objectMap.putAll(getContext().get().contextMap);
215208
}
216209
map.forEach((key, value) -> {
217210
if (value == null || (value instanceof String && ((String) value).isEmpty())) {
218-
renderableMap.remove(key);
211+
objectMap.remove(key);
219212
} else {
220-
renderableMap.put(
221-
key, value instanceof Renderable ? (Renderable) value : new ObjectRenderable(value));
213+
objectMap.put(key, value);
222214
}
223215
});
224-
return new Instance(renderableMap);
216+
return new Instance(objectMap);
225217
} else {
226218
return getContext().isPresent() ? getContext().get() : new Instance();
227219
}
@@ -235,15 +227,14 @@ public static Instance where(Map<String, ?> map) {
235227
*/
236228
public static void runWhere(String key, Object obj, Runnable op) {
237229
if (obj != null) {
238-
Renderable renderable = obj instanceof Renderable ? (Renderable) obj : new ObjectRenderable(obj);
239-
Map<String, Renderable> map = new HashMap<>();
230+
Map<String, Object> map = new HashMap<>();
240231
if (getContext().isPresent()) {
241232
map.putAll(getContext().get().contextMap);
242233
}
243-
map.put(key, renderable);
234+
map.put(key, obj);
244235
new Instance(map).run(op);
245236
} else {
246-
Map<String, Renderable> map = new HashMap<>();
237+
Map<String, Object> map = new HashMap<>();
247238
if (getContext().isPresent()) {
248239
map.putAll(getContext().get().contextMap);
249240
}
@@ -261,12 +252,11 @@ public static void runWhere(String key, Object obj, Runnable op) {
261252
*/
262253
public static Future<?> runWhere(String key, Object obj, ExecutorService executorService, Runnable op) {
263254
if (obj != null) {
264-
Renderable renderable = obj instanceof Renderable ? (Renderable) obj : new ObjectRenderable(obj);
265-
Map<String, Renderable> map = new HashMap<>();
255+
Map<String, Object> map = new HashMap<>();
266256
if (getContext().isPresent()) {
267257
map.putAll(getContext().get().contextMap);
268258
}
269-
map.put(key, renderable);
259+
map.put(key, obj);
270260
if (executorService != null) {
271261
return executorService.submit(new Runner(
272262
new Instance(map), ThreadContext.getContext(), ThreadContext.getImmutableStack(), op));
@@ -275,7 +265,7 @@ public static Future<?> runWhere(String key, Object obj, ExecutorService executo
275265
return CompletableFuture.completedFuture(0);
276266
}
277267
} else {
278-
Map<String, Renderable> map = new HashMap<>();
268+
Map<String, Object> map = new HashMap<>();
279269
if (getContext().isPresent()) {
280270
map.putAll(getContext().get().contextMap);
281271
}
@@ -297,14 +287,12 @@ public static Future<?> runWhere(String key, Object obj, ExecutorService executo
297287
*/
298288
public static void runWhere(Map<String, ?> map, Runnable op) {
299289
if (map != null && !map.isEmpty()) {
300-
Map<String, Renderable> renderableMap = new HashMap<>();
290+
Map<String, Object> objectMap = new HashMap<>();
301291
if (getContext().isPresent()) {
302-
renderableMap.putAll(getContext().get().contextMap);
292+
objectMap.putAll(getContext().get().contextMap);
303293
}
304-
map.forEach((key, value) -> {
305-
renderableMap.put(key, value instanceof Renderable ? (Renderable) value : new ObjectRenderable(value));
306-
});
307-
new Instance(renderableMap).run(op);
294+
objectMap.putAll(map);
295+
new Instance(objectMap).run(op);
308296
} else {
309297
op.run();
310298
}
@@ -318,15 +306,14 @@ public static void runWhere(Map<String, ?> map, Runnable op) {
318306
*/
319307
public static <R> R callWhere(String key, Object obj, Callable<R> op) throws Exception {
320308
if (obj != null) {
321-
Renderable renderable = obj instanceof Renderable ? (Renderable) obj : new ObjectRenderable(obj);
322-
Map<String, Renderable> map = new HashMap<>();
309+
Map<String, Object> map = new HashMap<>();
323310
if (getContext().isPresent()) {
324311
map.putAll(getContext().get().contextMap);
325312
}
326-
map.put(key, renderable);
313+
map.put(key, obj);
327314
return new Instance(map).call(op);
328315
} else {
329-
Map<String, Renderable> map = new HashMap<>();
316+
Map<String, Object> map = new HashMap<>();
330317
if (getContext().isPresent()) {
331318
map.putAll(getContext().get().contextMap);
332319
}
@@ -345,12 +332,11 @@ public static <R> R callWhere(String key, Object obj, Callable<R> op) throws Exc
345332
public static <R> Future<R> callWhere(String key, Object obj, ExecutorService executorService, Callable<R> op)
346333
throws Exception {
347334
if (obj != null) {
348-
Renderable renderable = obj instanceof Renderable ? (Renderable) obj : new ObjectRenderable(obj);
349-
Map<String, Renderable> map = new HashMap<>();
335+
Map<String, Object> map = new HashMap<>();
350336
if (getContext().isPresent()) {
351337
map.putAll(getContext().get().contextMap);
352338
}
353-
map.put(key, renderable);
339+
map.put(key, obj);
354340
if (executorService != null) {
355341
return executorService.submit(new Caller<R>(
356342
new Instance(map), ThreadContext.getContext(), ThreadContext.getImmutableStack(), op));
@@ -360,7 +346,7 @@ public static <R> Future<R> callWhere(String key, Object obj, ExecutorService ex
360346
}
361347
} else {
362348
if (executorService != null) {
363-
Map<String, Renderable> map = new HashMap<>();
349+
Map<String, Object> map = new HashMap<>();
364350
if (getContext().isPresent()) {
365351
map.putAll(getContext().get().contextMap);
366352
}
@@ -381,14 +367,12 @@ public static <R> Future<R> callWhere(String key, Object obj, ExecutorService ex
381367
*/
382368
public static <R> R callWhere(Map<String, ?> map, Callable<R> op) throws Exception {
383369
if (map != null && !map.isEmpty()) {
384-
Map<String, Renderable> renderableMap = new HashMap<>();
370+
Map<String, Object> objectMap = new HashMap<>();
385371
if (getContext().isPresent()) {
386-
renderableMap.putAll(getContext().get().contextMap);
372+
objectMap.putAll(getContext().get().contextMap);
387373
}
388-
map.forEach((key, value) -> {
389-
renderableMap.put(key, value instanceof Renderable ? (Renderable) value : new ObjectRenderable(value));
390-
});
391-
return new Instance(renderableMap).call(op);
374+
objectMap.putAll(map);
375+
return new Instance(objectMap).call(op);
392376
} else {
393377
return op.call();
394378
}
@@ -398,8 +382,8 @@ public static class Instance {
398382

399383
private final Instance parent;
400384
private final String key;
401-
private final Renderable value;
402-
private final Map<String, Renderable> contextMap;
385+
private final Object value;
386+
private final Map<String, Object> contextMap;
403387

404388
private Instance() {
405389
this.parent = null;
@@ -408,14 +392,14 @@ private Instance() {
408392
this.contextMap = null;
409393
}
410394

411-
private Instance(Map<String, Renderable> map) {
395+
private Instance(Map<String, Object> map) {
412396
this.parent = null;
413397
this.key = null;
414398
this.value = null;
415399
this.contextMap = map;
416400
}
417401

418-
private Instance(Instance parent, String key, Renderable value) {
402+
private Instance(Instance parent, String key, Object value) {
419403
this.parent = parent;
420404
this.key = key;
421405
this.value = value;
@@ -446,8 +430,7 @@ public Instance where(String key, Supplier<Object> supplier) {
446430

447431
private Instance addObject(String key, Object obj) {
448432
if (obj != null) {
449-
Renderable renderable = obj instanceof Renderable ? (Renderable) obj : new ObjectRenderable(obj);
450-
return new Instance(this, key, renderable);
433+
return new Instance(this, key, obj);
451434
}
452435
return this;
453436
}
@@ -495,7 +478,7 @@ public <R> Future<R> call(ExecutorService executorService, Callable<R> op) {
495478
}
496479

497480
private static class Runner implements Runnable {
498-
private final Map<String, Renderable> contextMap = new HashMap<>();
481+
private final Map<String, Object> contextMap = new HashMap<>();
499482
private final Map<String, String> threadContextMap;
500483
private final ThreadContext.ContextStack contextStack;
501484
private final Instance context;
@@ -546,7 +529,7 @@ public void run() {
546529
}
547530

548531
private static class Caller<R> implements Callable<R> {
549-
private final Map<String, Renderable> contextMap = new HashMap<>();
532+
private final Map<String, Object> contextMap = new HashMap<>();
550533
private final Instance context;
551534
private final Map<String, String> threadContextMap;
552535
private final ThreadContext.ContextStack contextStack;
@@ -595,43 +578,4 @@ public R call() throws Exception {
595578
}
596579
}
597580
}
598-
599-
/**
600-
* Interface for converting Objects stored in the ContextScope to Strings for logging.
601-
*
602-
* Users implementing this interface are encouraged to make the render method as lightweight as possible,
603-
* Typically by creating the String representation of the object during its construction and just returning
604-
* the String.
605-
*/
606-
public static interface Renderable {
607-
/**
608-
* Render the object as a String.
609-
* @return the String representation of the Object.
610-
*/
611-
default String render() {
612-
return this.toString();
613-
}
614-
615-
default Object getObject() {
616-
return this;
617-
}
618-
}
619-
620-
private static class ObjectRenderable implements Renderable {
621-
private final Object object;
622-
623-
public ObjectRenderable(Object object) {
624-
this.object = object;
625-
}
626-
627-
@Override
628-
public String render() {
629-
return object.toString();
630-
}
631-
632-
@Override
633-
public Object getObject() {
634-
return object;
635-
}
636-
}
637581
}

log4j-api/src/main/java/org/apache/logging/log4j/simple/SimpleLogger.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ public void logMessage(
297297
sb.append(msg.getFormattedMessage());
298298
if (showContextMap) {
299299
final Map<String, String> mdc = new HashMap<>(ThreadContext.getImmutableContext());
300-
ScopedContext.getContextMap().forEach((key, value) -> mdc.put(key, value.render()));
300+
ScopedContext.getContextMap().forEach((key, value) -> mdc.put(key, value.toString()));
301301
if (!mdc.isEmpty()) {
302302
sb.append(SPACE);
303303
sb.append(mdc.toString());

log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ScopedContextDataProvider.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ public String get(String key) {
3838

3939
@Override
4040
public Map<String, String> supplyContextData() {
41-
Map<String, ScopedContext.Renderable> contextMap = ScopedContext.getContextMap();
41+
Map<String, Object> contextMap = ScopedContext.getContextMap();
4242
if (!contextMap.isEmpty()) {
4343
Map<String, String> map = new HashMap<>();
44-
contextMap.forEach((key, value) -> map.put(key, value.render()));
44+
contextMap.forEach((key, value) -> map.put(key, value.toString()));
4545
return map;
4646
} else {
4747
return Collections.emptyMap();

0 commit comments

Comments
 (0)