qos-ch / slf4j

Simple Logging Facade for Java
http://www.slf4j.org
MIT License
2.35k stars 990 forks source link

SLF4J-522 - Adds NDC.contains() Method #267

Open mwootendev opened 3 years ago

mwootendev commented 3 years ago

Updates the NDC class in slf4j-ext to include a static contains() method that can check the existing nested diagnostic context for the presence of a value.

Added unit tests to test the new NDC.contains() method.

ceki commented 3 years ago

@mwootendev Can you kindly describe what type of problems are better solved by NDC instead of MDC?

mwootendev commented 3 years ago

@ceki I've actually found the NDC useful for tracing a path through a system. It serves as a good "how did we get here", especially when there are multiple paths that interface with some common services. For instance, if there is a low-level service for looking up a user, there will probably be multiple higher-level parts of the system that utilize it. If each layer pushes its own NDC context you get a fairly accurate picture of how you got there. For instance, the same logging statement in that user service may end up with different NDCs of [ORDERS USER LOOKUP], [USER REGISTRATION LOOKUP], and [USER ACTIVATION LOOKUP] depending on how that service was reached. This is especially useful for log statements that are not errors and do not have a stack trace.

I tend to differentiate MDC as the "what did we get here", like an order number or user ID. Having the context of "how" can often greatly enhance the value of the "what."

ceki commented 3 years ago

@mwootendev OK, I am still trying to understand. So [ORDERS USER LOOKUP], [USER REGISTRATION LOOKUP], and [USER ACTIVATION LOOKUP] are NDC values?

If so, [ORDERS USER LOOKUP] describes the ORDERS -> USER -> LOOKUP path?

mwootendev commented 3 years ago

@ceki Yes, that would be correct.