You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by "kertzi (via GitHub)" <gi...@apache.org> on 2024/01/18 11:26:37 UTC
[I] Ability to add metadata to all log event created by logger (logging-log4j2)
kertzi opened a new issue, #2213:
URL: https://github.com/apache/logging-log4j2/issues/2213
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:
```
class App {
void startWorkAtWorker() {
Context context;
context.logger = LogManager.getLogger("whatever");
context.logger.addMetadata("key", "value");
Worker WorkerInstance;
WorkerInstance.queue(context)
}
}
class Worker {
void runTask() {
// pick context from queue....
// run task
context.logger.info("starting work, this event has metadata, pushed when context created");
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] Ability to add metadata to all log event created by logger (logging-log4j2)
Posted by "vy (via GitHub)" <gi...@apache.org>.
vy commented on issue #2213:
URL: https://github.com/apache/logging-log4j2/issues/2213#issuecomment-1898520409
I certainly recognize the problem. It is a 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 `Logger` with an extra field... Let's say we have implemented that. How will the downstream use it? We need to adapt all layouts to take this new field into account too. Hence, _"a new field to `Logger`"_ has far reaching implications.
### Using `MapMessage`
An approach you can carry yourself out is to use `MapMessage`s instead:
```
context.addMetadata("githubUserId", "vy");
logger.info(new MapMessage(Map.of(
"message", "comment submitted",
"metadata", context.getMetadata())));
```
This is an area where Log4j shines compared to other logging solutions in the market: You can log anything that implements the `Message` interface, you are not limited to `String`s.
### Injecting a dynamic MDC entry
You can try injecting into MDC an `Object` whose `toString()` will return (or `StringBuilderFormattable#formatTo(StringBuilder)` will render) the metadata stored in the `TaskContext`. Something like this might work:
```
private volatile TaskContext taskContext;
private void initMdc() {
ObjectThreadContextMap map = (ObjectThreadContextMap) ThreadContext.getThreadContextMap();
map.put("taskMetadata", (StringBuilderFormattable) (buffer -> buffer.append(taskContext.getMetadata())));
}
private void performTask() {
taskContext.addMetadata("foo", "bar");
logger.info("buzz");
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] Ability to add metadata to all log event created by logger (logging-log4j2)
Posted by "vy (via GitHub)" <gi...@apache.org>.
vy closed issue #2213: Ability to add metadata to all log event created by logger
URL: https://github.com/apache/logging-log4j2/issues/2213
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org