Skip to content

Commit e6df7e2

Browse files
committed
Document new Slf4j propagation functionality.
This closes #590 Signed-off-by: Sjoerd Talsma <sjoerdtalsma@users.noreply.github.com>
1 parent 2b46ffd commit e6df7e2

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
lines changed

managers/context-manager-slf4j/README.md renamed to managers/context-manager-slf4j/readme.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[![Maven Version][maven-img]][maven]
22

3-
# SLF4J MDC propagation library
3+
# Slf4j MDC propagation library
44

55
Adding the `slf4j-propagation` jar to your classpath
66
is all that is needed to let the [Mapped Diagnostic Context (MDC)][mdc]
@@ -20,11 +20,15 @@ Add it to your classpath.
2020

2121
Done!
2222

23-
Now the `MDC.getCopyOfContextMap()` is copied into each snapshot
24-
from the `ContextSnapshot.capture()` method
25-
to be reactivated by the `Contextsnapshot.reactivate()` call.
26-
The `ContextAwareExecutorService` automatically propagates the full [MDC] content
27-
into all executed tasks this way.
23+
## What does it do?
24+
25+
The current `MDC` map values are included in each `ContextSnapshot` when it is captured.
26+
Upon reactivation, these captured values (and _only_ these captured values) are reactivated in the MDC.
27+
- MDC keys that exist in the target MDC which are _not_ part of the snapshot are left unchanged.
28+
- MDC keys with the case-insensitive substring `"thread"`, are _not_ captured in the ContextSnashot,
29+
since they most-likely contain a thread-specific value.
30+
- When a reactivation is _closed_, the _previous_ MDC values _for the captured keys_ are restored.
31+
All other keys that are _not_ part of the context snapshot will be left unchanged.
2832

2933

3034
[maven-img]: https://img.shields.io/maven-central/v/nl.talsmasoftware.context.managers/context-manager-slf4j

managers/context-manager-slf4j/src/main/java/nl/talsmasoftware/context/managers/slf4j/mdc/Slf4jMdcManager.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import nl.talsmasoftware.context.api.Context;
1919
import nl.talsmasoftware.context.api.ContextManager;
20+
import nl.talsmasoftware.context.api.ContextSnapshot;
2021
import org.slf4j.MDC;
2122

2223
import java.util.Collections;
@@ -30,18 +31,28 @@
3031
* getting the active context is fully delegated to the MDC.
3132
*
3233
* <p>
34+
* Upon {@linkplain ContextSnapshot#reactivate() reactivation},
35+
* these captured values (and <em>only</em> these captured values) are reactivated in the MDC.
36+
* <ul>
37+
* <li>MDC keys that exist in the target MDC which are <em>not</em> part of the snapshot are left unchanged.
38+
* <li>MDC keys with the case-insensitive substring {@code "thread"}, are <em>not</em> captured
39+
* in the {@linkplain ContextSnapshot},
40+
* since they most-likely contain a thread-specific value.
41+
* <li>When a reactivation is <em>closed</em>, the <em>previous</em> MDC values <em>for the captured keys</em> are restored.
42+
* All other keys that are not part of the context snapshot will be left unchanged.
43+
* </ul>
44+
*
45+
* <p>
3346
* Closing a context returned form {@link #activate(Map)} restores the MDC
3447
* to the values it had before the context was created.<br>
3548
* This means that closing nested contexts out-of-order will probably result in an undesirable state.<br>
3649
* It is therefore strongly advised to use Java's {@code try-with-resources} mechanism to ensure proper
3750
* closing of nested MDC contexts.
3851
*
39-
* <p>
40-
* This manager does not implement the optional {@link #clear()} method.
52+
* @author Sjoerd Talsma
53+
* @implNote This manager does not implement the optional {@link #clear()} method.
4154
* {@linkplain ContextManager#clearAll()} will therefore <strong>not</strong> clear the {@linkplain MDC}.
4255
* Please use {@linkplain MDC#clear()} explicitly to do that.
43-
*
44-
* @author Sjoerd Talsma
4556
*/
4657
public class Slf4jMdcManager implements ContextManager<Map<String, String>> {
4758
/**

managers/context-manager-slf4j/src/main/java/nl/talsmasoftware/context/managers/slf4j/mdc/package-info.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,19 @@
1818
*
1919
* <p>
2020
* This context manager maintains no ThreadLocal state of its own,
21-
* instead it propagates the
22-
* existing {@linkplain org.slf4j.MDC#getCopyOfContextMap() MDC context map}
23-
* into each {@linkplain nl.talsmasoftware.context.api.ContextSnapshot context snapshot}
24-
* to be applied again when the snapshot
25-
* is {@linkplain nl.talsmasoftware.context.api.ContextSnapshot#reactivate() reactivated}
26-
* in another thread.
21+
* instead it propagates the current {@linkplain org.slf4j.MDC MDC} values
22+
* into each {@linkplain nl.talsmasoftware.context.api.ContextSnapshot context snapshot} that is captured.
23+
*
24+
* <p>
25+
* Upon {@linkplain nl.talsmasoftware.context.api.ContextSnapshot#reactivate() reactivation},
26+
* these captured values (and <em>only</em> these captured values) are reactivated in the MDC.
27+
* <ul>
28+
* <li>MDC keys that exist in the target MDC which are <em>not</em> part of the snapshot are left unchanged.
29+
* <li>MDC keys with the case-insensitive substring {@code "thread"}, are <em>not</em> captured
30+
* in the {@linkplain nl.talsmasoftware.context.api.ContextSnapshot},
31+
* since they most-likely contain a thread-specific value.
32+
* <li>When a reactivation is <em>closed</em>, the <em>previous</em> MDC values <em>for the captured keys</em> are restored.
33+
* All other keys that are not part of the context snapshot will be left unchanged.
34+
* </ul>
2735
*/
2836
package nl.talsmasoftware.context.managers.slf4j.mdc;

0 commit comments

Comments
 (0)