Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose copy of map of deques in LogbackMDCAdapter #795

Open
alexanderjastram opened this issue Mar 22, 2024 · 4 comments
Open

Expose copy of map of deques in LogbackMDCAdapter #795

alexanderjastram opened this issue Mar 22, 2024 · 4 comments

Comments

@alexanderjastram
Copy link

alexanderjastram commented Mar 22, 2024

Currently, only the regular property map is exposed in the MDCAdapter, and formatters such as the LogStashFormatter use the properties defined there to add additional tags to logs. This makes it impossible to have stack-based logger context, since children can potentially overwrite the context of their parents.

For example:

class LoggingExample {
  main() {
      MDC.put('key', 'valueParent');
      subMethod();
      log.info('parent log message');
  }

  subMethod() {
      MDC.put('key', 'valueChild');
      log.info('child log message');
  }

}

would result in the following logs:

> child log message key:valueChild
> parent log message key:valueChild

Now using the dequed properties we can keep track of the parent property instead of overwriting it:

class LoggingExample {
  main() {
      MDC.pushByKey('key', 'valueParent');
      subMethod();
      log.info('parentLog');
      MDC.popByKey('key')
  }

  subMethod() {
      MDC.pushByKey('key', 'valueChild');
      log.info('childLog');
      MDC.popByKey('key')
  }

}

Ideally this would result in the following logs:

> child log message key:valueChild
> parent log message key:valueParent

Unfortunately, the key value pairs of the dequeue map are not exposed, so the example above cannot be implemented. If it was exposed we could make a custom JSONProvider that provides the tags from the dequed map.

@ceki
Copy link
Member

ceki commented Mar 22, 2024

@alexanderjastram Thank you for this report. Could you please provide a patch or a PR?

@alexanderjastram
Copy link
Author

will do - I'll post it here once its done

@ceki
Copy link
Member

ceki commented Mar 22, 2024

@alexanderjastram No need to provide a full implementation. Just the gist of the api you wish to see.

@alexanderjastram
Copy link
Author

@ceki it requires 2 pull requests, since we also need to expose the copy in the ThreadLocalMapOfStacks:
slf4j
logback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants