Replies: 9 comments 16 replies
-
The concept seems ok, but the name Also the name The downside of the term "scope" is that it is also used by the upcoming Scoped Values feature. So if there is ever a future where the MDC (or something like it) is a Naming is hard. |
Beta Was this translation helpful? Give feedback.
-
Could we use something like a Runnable instead of a helper? public static void withMDC(Map<String, String> ctx, Runnable lambda) {
var previousCtx = MDC.getCopyOfContextMap();
try {
MDC.clear();
MDC.setContextMap(ctx);
lambda.run();
} finally {
MDC.clear();
MDC.setContextMap(previousCtx);
}
}
public static void withMDC(Runnable lambda) {
with(Map.of(), lambda);
} withMDC(() -> {
MDC.put("some", "thing");
}); |
Beta Was this translation helpful? Give feedback.
-
I think this is a great addition. One thing I would consider is making it extendable/implementable by the libraries implementing SLF4J. It could potentially replace MDC itself and allow ScopedValues in newer JVM instead of a ThreadLocal, which can be problematic with virtual threads. Additionally, I'd second having a method that takes a runnable, but maybe, to preserve the additive property, prepare the state outside and only apply before running the code block, cleaning up right after... |
Beta Was this translation helpful? Give feedback.
-
Love this API. I have two requests from a devx perspective: First, it would be great if MDCAmbit mdcAmbit = new MDCAmbit();
// populate here
try {
// logic here
} finally {
mdcAmbit.clear();
} we could just do this: try(MDCAmbit mdcAmbit = new MDCAmbit()) {
// populate here
// logic here
} Second, it would be awesome if the API provided a builder API so that we can do this: MDCAmbit mdcAmbit = MDCAmbit.builder()
.put("k0", value0)
.put("k1", value1)
.build(); If you were to combine the try(MDCAmbit mdcAmbit = MDCAmbit.builder()
.put("k0", value0)
.put("k1", value1)
.build()) {
// logic here
} |
Beta Was this translation helpful? Give feedback.
-
This (A small question: Is it accurate to say that |
Beta Was this translation helpful? Give feedback.
-
The ticket #404 is also relevant to |
Beta Was this translation helpful? Give feedback.
-
I think it critical for |
Beta Was this translation helpful? Give feedback.
-
Maybe that's because I am not a native English speaker, but what does |
Beta Was this translation helpful? Give feedback.
-
There's a typo in the javadoc for this class: I'd suggest:
|
Beta Was this translation helpful? Give feedback.
-
MDCClosable
was supposed to help users remove entries at the end of a try/catch block. Unfortunately, as discussed in SLF4J-557 closable instances, includingMDCCloseable
instances are closed before the catch clause. This is very counter-intuitive and most probably goes against the wishes of the developer.To alleviate this problem,
MDCAmbit
is being proposed under thebranch_2.1.x
branch.Usage examples can be seen in the javadocs of
MDCAmbit
and well as inMDCAmbitTest
Given that this
MDCAmbit
is slated to be added to theorg.slf4j
package, your feedback is requested.For example, assuming
MDCAmbit
is added to theorg.slf4j
package, what should be the SLF4J release version? Should it be version 2.0.10, 2.1 or 3.0?Beta Was this translation helpful? Give feedback.
All reactions