Skip to content

Commit f95b50b

Browse files
authored
Add scoped context support. Remove ThreadContextDataInjector. (#2494)
* Add ScopedContext. Remove ThreadContextDataInjector * Import references in javadoc
1 parent 16511c9 commit f95b50b

34 files changed

+623
-834
lines changed

log4j-async-logger/src/main/java/org/apache/logging/log4j/async/logger/AsyncLogger.java

+3-17
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.apache.logging.log4j.Level;
2121
import org.apache.logging.log4j.Marker;
2222
import org.apache.logging.log4j.ThreadContext;
23-
import org.apache.logging.log4j.core.ContextDataInjector;
2423
import org.apache.logging.log4j.core.Logger;
2524
import org.apache.logging.log4j.core.LoggerContext;
2625
import org.apache.logging.log4j.core.ReusableLogEvent;
@@ -30,7 +29,7 @@
3029
import org.apache.logging.log4j.core.config.LoggerConfig;
3130
import org.apache.logging.log4j.core.config.Property;
3231
import org.apache.logging.log4j.core.config.ReliabilityStrategy;
33-
import org.apache.logging.log4j.core.impl.ContextDataFactory;
32+
import org.apache.logging.log4j.core.impl.ContextData;
3433
import org.apache.logging.log4j.core.time.Clock;
3534
import org.apache.logging.log4j.core.time.NanoClock;
3635
import org.apache.logging.log4j.kit.logger.AbstractLogger;
@@ -72,7 +71,6 @@ public class AsyncLogger extends Logger {
7271
// immediate inlining instead of waiting until they are designated "hot enough".
7372

7473
private final Clock clock; // not reconfigurable
75-
private final ContextDataInjector contextDataInjector; // not reconfigurable
7674

7775
private final Recycler<RingBufferLogEventTranslator> translatorRecycler;
7876
private final AsyncLoggerDisruptor loggerDisruptor;
@@ -105,7 +103,6 @@ public class AsyncLogger extends Logger {
105103
includeLocation = privateConfig.loggerConfig.isIncludeLocation();
106104
nanoClock = configuration.getNanoClock();
107105
clock = configuration.getComponent(Clock.KEY);
108-
contextDataInjector = configuration.getComponent(ContextDataInjector.KEY);
109106
}
110107

111108
/*
@@ -214,7 +211,6 @@ private void initTranslator(
214211
location,
215212
clock,
216213
nanoClock,
217-
contextDataInjector,
218214
requiresLocation());
219215
}
220216

@@ -255,7 +251,8 @@ public void actualAsyncLog(final ReusableLogEvent event) {
255251

256252
@SuppressWarnings("ForLoopReplaceableByForEach") // Avoid iterator allocation
257253
private void onPropertiesPresent(final ReusableLogEvent event, final List<Property> properties) {
258-
final StringMap contextData = getContextData(event);
254+
StringMap contextData = event.getContextData();
255+
ContextData.addAll(contextData);
259256
for (int i = 0, size = properties.size(); i < size; i++) {
260257
final Property prop = properties.get(i);
261258
if (contextData.getValue(prop.getName()) != null) {
@@ -266,17 +263,6 @@ private void onPropertiesPresent(final ReusableLogEvent event, final List<Proper
266263
: prop.getValue();
267264
contextData.putValue(prop.getName(), value);
268265
}
269-
event.setContextData(contextData);
270-
}
271-
272-
private static StringMap getContextData(final ReusableLogEvent event) {
273-
final StringMap contextData = event.getContextData();
274-
if (contextData.isFrozen()) {
275-
final StringMap temp = ContextDataFactory.createContextData();
276-
temp.putAll(contextData);
277-
return temp;
278-
}
279-
return contextData;
280266
}
281267

282268
// package-protected for tests

log4j-async-logger/src/main/java/org/apache/logging/log4j/async/logger/RingBufferLogEvent.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ public void setValues(
120120
this.marker = aMarker;
121121
this.fqcn = theFqcn;
122122
this.location = aLocation;
123-
this.contextData = mutableContextData;
123+
if (mutableContextData != null) {
124+
this.contextData = mutableContextData;
125+
}
124126
this.contextStack = aContextStack;
125127
this.asyncLogger = anAsyncLogger;
126128
this.populated = true;

log4j-async-logger/src/main/java/org/apache/logging/log4j/async/logger/RingBufferLogEventTranslator.java

+6-8
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import org.apache.logging.log4j.Level;
2121
import org.apache.logging.log4j.Marker;
2222
import org.apache.logging.log4j.ThreadContext.ContextStack;
23-
import org.apache.logging.log4j.core.ContextDataInjector;
23+
import org.apache.logging.log4j.core.impl.ContextData;
2424
import org.apache.logging.log4j.core.time.Clock;
2525
import org.apache.logging.log4j.core.time.NanoClock;
2626
import org.apache.logging.log4j.message.Message;
@@ -38,7 +38,6 @@
3838
@NullMarked
3939
public class RingBufferLogEventTranslator implements EventTranslator<RingBufferLogEvent> {
4040

41-
private ContextDataInjector contextDataInjector;
4241
private AsyncLogger asyncLogger;
4342
String loggerName;
4443
protected @Nullable Marker marker;
@@ -61,6 +60,9 @@ public class RingBufferLogEventTranslator implements EventTranslator<RingBufferL
6160
public void translateTo(final RingBufferLogEvent event, final long sequence) {
6261
try {
6362
final StringMap contextData = event.getContextData();
63+
if (contextData != null) {
64+
ContextData.addAll(contextData);
65+
}
6466
// Compute location if necessary
6567
event.setValues(
6668
asyncLogger,
@@ -70,9 +72,7 @@ public void translateTo(final RingBufferLogEvent event, final long sequence) {
7072
level,
7173
message,
7274
thrown,
73-
// config properties are taken care of in the EventHandler thread
74-
// in the AsyncLogger#actualAsyncLog method
75-
contextDataInjector.injectContextData(null, contextData),
75+
null,
7676
contextStack,
7777
threadId,
7878
threadName,
@@ -90,7 +90,7 @@ public void translateTo(final RingBufferLogEvent event, final long sequence) {
9090
* Release references held by this object to allow objects to be garbage-collected.
9191
*/
9292
void clear() {
93-
setBasicValues(null, null, null, null, null, null, null, null, null, null, null, null, false);
93+
setBasicValues(null, null, null, null, null, null, null, null, null, null, null, false);
9494
}
9595

9696
public void setBasicValues(
@@ -105,7 +105,6 @@ public void setBasicValues(
105105
final @Nullable StackTraceElement location,
106106
final Clock clock,
107107
final NanoClock nanoClock,
108-
final ContextDataInjector contextDataInjector,
109108
final boolean includeLocation) {
110109
this.asyncLogger = asyncLogger;
111110
this.loggerName = loggerName;
@@ -118,7 +117,6 @@ public void setBasicValues(
118117
this.location = location;
119118
this.clock = clock;
120119
this.nanoClock = nanoClock;
121-
this.contextDataInjector = contextDataInjector;
122120
this.requiresLocation = location == null && includeLocation;
123121
}
124122

log4j-core-test/src/test/java/org/apache/logging/log4j/core/LogEventFactoryTest.java

+10-9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.logging.log4j.Level;
2424
import org.apache.logging.log4j.Marker;
2525
import org.apache.logging.log4j.core.config.Property;
26+
import org.apache.logging.log4j.core.impl.ContextData;
2627
import org.apache.logging.log4j.core.impl.ContextDataFactory;
2728
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
2829
import org.apache.logging.log4j.core.impl.LogEventFactory;
@@ -31,7 +32,7 @@
3132
import org.apache.logging.log4j.core.test.junit.Named;
3233
import org.apache.logging.log4j.core.test.junit.TestBinding;
3334
import org.apache.logging.log4j.message.Message;
34-
import org.apache.logging.log4j.plugins.Inject;
35+
import org.apache.logging.log4j.util.StringMap;
3536
import org.junit.jupiter.api.Test;
3637

3738
/**
@@ -53,12 +54,6 @@ public void testEvent(@Named("List") final ListAppender app, final LoggerContext
5354
}
5455

5556
public static class TestLogEventFactory implements LogEventFactory {
56-
private final ContextDataInjector injector;
57-
58-
@Inject
59-
public TestLogEventFactory(final ContextDataInjector injector) {
60-
this.injector = injector;
61-
}
6257

6358
@Override
6459
public LogEvent createEvent(
@@ -69,14 +64,20 @@ public LogEvent createEvent(
6964
final Message data,
7065
final List<Property> properties,
7166
final Throwable t) {
67+
StringMap contextData = ContextDataFactory.createContextData();
68+
if (properties != null && !properties.isEmpty()) {
69+
for (Property property : properties) {
70+
contextData.putValue(property.getName(), property.getValue());
71+
}
72+
}
73+
ContextData.addAll(contextData);
7274
return Log4jLogEvent.newBuilder()
7375
.setLoggerName("Test")
7476
.setMarker(marker)
7577
.setLoggerFqcn(fqcn)
7678
.setLevel(level)
7779
.setMessage(data)
78-
.setContextDataInjector(injector)
79-
.setContextData(injector.injectContextData(properties, ContextDataFactory.createContextData()))
80+
.setContextData(contextData)
8081
.setThrown(t)
8182
.build();
8283
}

log4j-core-test/src/test/java/org/apache/logging/log4j/core/impl/ThreadContextDataInjectorTest.java

-178
This file was deleted.

0 commit comments

Comments
 (0)