Skip to content

Commit fefd16b

Browse files
committed
Rewrite thread-context.adoc et al. (#2535)
1 parent 4426dd4 commit fefd16b

File tree

5 files changed

+114
-219
lines changed

5 files changed

+114
-219
lines changed

Diff for: src/site/antora/modules/ROOT/pages/manual/api.adoc

+22-8
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ xref:manual/logbuilder.adoc[Read more on the Fluent API...]
239239
[#fish-tagging]
240240
== Fish tagging
241241
242-
Just as a fish can be tagged and have its movement tracked (aka. _fish tagging_), stamping log events with a common tag or set of data
242+
Just as a fish can be tagged and have its movement tracked (aka. _fish tagging_ footnote:[Fish tagging is first described by Neil Harrison in the _"Patterns for Logging Diagnostic Messages"_ chapter of https://dl.acm.org/doi/10.5555/273448[_"Pattern Languages of Program Design 3"_ edited by R. Martin, D. Riehle, and F. Buschmann in 1997].]), stamping log events with a common tag or set of data
243243
elements allows the complete flow of a transaction or a request to be tracked.
244244
You can use them for several purposes, such as:
245245
@@ -283,7 +283,7 @@ xref:manual/markers.adoc[Read more on markers...]
283283
[#scoped-context]
284284
=== Scoped Context
285285
286-
Just like a https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/lang/ScopedValue.html[Java's `ScopedValue`], in Scoped Context, the visibility of tags are associated with the block they were introduced:
286+
Just like a https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/lang/ScopedValue.html[Java's `ScopedValue`], _Scoped Context_ facilitates associating information with a certain block of code and makings this accessible to the rest of the logging system:
287287
288288
[source,java]
289289
----
@@ -317,22 +317,36 @@ xref:manual/scoped-context.adoc[Read more on Scoped Context...]
317317
[#thread-context]
318318
=== Thread Context
319319
320-
Just like https://docs.oracle.com/javase/8/docs/api/java/lang/ThreadLocal.html[Java's `ThreadLocal`], in Thread Context, the visibility of tags are associated with the thread they were introduced.
321-
Thread Context offers both a map-structured, called _Mapped Diagnostic Context (MDC)_, and a stack-structured, called _Nested Diagnostic Context (NDC)_, storage:
320+
Just like https://docs.oracle.com/javase/8/docs/api/java/lang/ThreadLocal.html[Java's `ThreadLocal`], _Thread Context_ facilitates associating information with the executing thread and making this information accessible to the rest of the logging system.
321+
Thread Context offers both
322+
323+
* map-structured – referred to as _Thread Context Map_ or _Mapped Diagnostic Context (MDC)_
324+
* stack-structured – referred to as _Thread Context Stack_ or _Nested Diagnostic Context (NDC)_
325+
326+
storage:
322327
323328
[source,java]
324329
----
325330
ThreadContext.put("ipAddress", request.getRemoteAddr()); // <1>
326331
ThreadContext.put("hostName", request.getServerName()); // <1>
327332
ThreadContext.put("loginId", session.getAttribute("loginId")); // <1>
328333
329-
ThreadContext.push("performWork()"); // <2>
334+
void performWork() {
335+
ThreadContext.push("performWork()"); // <2>
336+
337+
LOGGER.debug("Performing work"); // <3>
338+
// Perform the work
339+
340+
ThreadContext.pop(); // <4>
341+
}
330342
331-
LOGGER.debug("Performing work"); // <3>
343+
ThreadContext.clear(); // <5>
332344
----
333-
<1> Associating properties with the logging context map of the thread
334-
<2> Pushing properties to the logging context stack of the thread
345+
<1> Adding properties to the thread context map
346+
<2> Pushing properties to the thread context stack
335347
<3> Added properties can later on be used to, for instance, filter the log event, provide extra information in the layout, etc.
348+
<4> Popping the last pushed property from the thread context stack
349+
<5> Clearing the thread context (for both stack and map!)
336350
337351
[CAUTION]
338352
====

Diff for: src/site/antora/modules/ROOT/pages/manual/extending.adoc

-26
Original file line numberDiff line numberDiff line change
@@ -580,32 +580,6 @@ ListAppender list1 = ListAppender.createAppender("List1", true, false, null, nul
580580
ListAppender list2 = ListAppender.newBuilder().setName("List1").setEntryPerNewLine(true).build();
581581
----
582582
583-
[#Custom_ContextDataProvider]
584-
== Custom ContextDataProvider
585-
586-
The link:../javadoc/log4j-core/org/apache/logging/log4j/core/util/ContextDataProvider.html[`ContextDataProvider`]
587-
(introduced in Log4j 2.13.2) is an interface applications and libraries can use to inject
588-
additional key-value pairs into the LogEvent's context data. Log4j
589-
uses `java.util.ServiceLoader` to locate and load `ContextDataProvider` instances.
590-
Log4j itself adds the ThreadContext data to the LogEvent using
591-
`org.apache.logging.log4j.core.impl.ThreadContextDataProvider`. Custom implementations
592-
should implement the `org.apache.logging.log4j.core.util.ContextDataProvider` interface and
593-
declare it as a service by defining the implmentation class in a file named
594-
`META-INF/services/org.apache.logging.log4j.core.util.ContextDataProvider`.
595-
596-
== Custom ThreadContextMap implementations
597-
598-
A garbage-free `StringMap`-based context map can be installed by setting
599-
system property `log4j2.garbagefreeThreadContextMap` to true.
600-
601-
Any custom link:../javadoc/log4j-core/org/apache/logging/log4j/spi/ThreadContextMap.html[`ThreadContextMap`]
602-
implementation can be installed by setting system property `log4j2.threadContextMap`
603-
to the fully qualified class name of the class implementing the `ThreadContextMap`
604-
interface. By also implementing the `ReadOnlyThreadContextMap` interface, your custom
605-
`ThreadContextMap` implementation will be accessible to applications via the
606-
link:../javadoc/log4j-api/org/apache/logging/log4j/ThreadContext.html#getThreadContextMap()[`ThreadContext::getThreadContextMap`]
607-
method.
608-
609583
[#Custom_Plugins]
610584
== Custom Plugins
611585

Diff for: src/site/antora/modules/ROOT/pages/manual/garbagefree.adoc

+2-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ The `Unbox.box(primitive)` methods write directly into a `StringBuilder`, and th
209209
Not all Log4j API feature set is garbage-free, specifically:
210210
211211
* The `ThreadContext` map (aka. MDC) is not garbage-free by default, but can be configured to be garbage-free by setting xref:#log4j2.garbagefreeThreadContextMap[the `log4j2.garbagefreeThreadContextMap` system property] to `true`.
212-
* The `ThreadContext` stack is not garbage-free.
212+
* The `ThreadContext` stack (aka. NDC) is not garbage-free.
213+
* xref:manual/scoped-context.adoc[] is not garbage-free.
213214
* Logging very large messages (i.e., more than xref:#log4j2.maxReusableMsgSize[`log4j2.maxReusableMsgSize`] characters, which defaults to 518), when all loggers are xref:manual/async.adoc[asynchronous loggers], will cause the internal `StringBuilder` in the
214215
`RingBuffer` to be trimmed back to their configured maximum size.
215216
* Logging messages containing `$\{variable}` substitutions creates temporary objects.

0 commit comments

Comments
 (0)