Replies: 5 comments 3 replies
-
I certainly recognize the problem. It is pretty common in reactive applications. The arrival of Loom and scoped values adds extra dimensions to it. Delivering a solution that only fixes the issue for one might jeopardize delivering a good fix for the others. I doubt if there is any one-size-fits-all answer to this. I personally expect MDC/NDC to be populated from scoped values in the future. Coming back to your proposal of enriching the Using
|
Beta Was this translation helpful? Give feedback.
-
Thanks for your prompt input! My current solution is (I'm using slf4j as for logging api) to create custom implementation (based on implementation you guys has done) for slf4j which adds my metadata to MDC in each log call:
Then in app code I just push metadata to logger:
It would be convinient to has this kind of thing possible directly from log4j2 framework but I understand it has far reaching implications. |
Beta Was this translation helpful? Give feedback.
-
Actually, you can create a ContextDataProvider to deal with this. Just store the data in the ContextDataProvider when you create it. However, the ContextDataProvider is NOT specific to a Logger. But this is similar to your approach of adding data to the MDC before each logging call except that this does NOT add it to the MDC but to the ThreadContextMap in the LogEvent. |
Beta Was this translation helpful? Give feedback.
-
Please see #2385 as this should do what you want. You can now do:
or you can do
In addition, nesting is supported as are arbitrary objects. To include the key/value pairs of the parent you would do:
If the object implements the Renderable interface then it can control the formatting of the object. Otherwise, toString() is used. |
Beta Was this translation helpful? Give feedback.
-
I think we should discuss it in |
Beta Was this translation helpful? Give feedback.
-
Some use cases it would be very beneficial to be able to attach metadata to logger created in some class.
There is MDC and NDC but those are global for thread and are not very good option in async application where some code maybe shared by same thread, for example application which uses workers and each job what workers does have own context and in that context there is logger which is used to log event specific for that job/context. In that case it would be way more better to attach needed metadata directly to logger.
Some example by pseudo code:
Beta Was this translation helpful? Give feedback.
All reactions