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

feat: pass logger/child logger as param to mixin #1709

Merged
merged 1 commit into from May 5, 2023

Conversation

mmarchini
Copy link
Contributor

Passing the logger or child logger as a parameter to mixin allows users to set logger-specific context in the logger object that can be used by mixin to enrich the context to be added to the resulting JSON.

One example use case for this is avoiding the "duplicate keys" caveat that comes with child loggers. If the user wants to make a known key "mergeable", they could add a custom function that concatenates values to the key and then use mixin to pass that merged value to the context. For example:

instance = pino({
  mixin(obj, num, logger) {
    return {
      "tags": logger.tags
    }
  }
})
instance.tags = {}

instance.addTag = function (key, value) {
  child.tags[key] = value
}

function createChild (logger, ...context) {
  const child = logger.child(...context)
  child.tags = { ...logger.tags }
  child.addTag = function (key, value) {
    child.tags[key] = value
  }
  return child
}

instance.addTag('foo', 1)
child.addTag('bar', 2)
instance.info('this will only have `foo: 1`')
child.info('this will have both `foo: 1` and `bar: 2`')

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! Can you update the docs too ?

Passing the logger or child logger as a parameter to `mixin` allows
users to set logger-specific context in the logger object that can be
used by `mixin` to enrich the context to be added to the resulting JSON.

One example use case for this is avoiding the "duplicate keys" caveat
that comes with child loggers. If the user wants to make a known key
"mergeable", they could add a custom function that concatenates values
to the key and then use `mixin` to pass that merged value to the
context. For example:

```js
const logger = pino({
  mixin (obj, num, logger) {
    return {
      tags: logger.tags
    }
  }
})
logger.tags = {}

logger.addTag = function (key, value) {
  logger.tags[key] = value
}

function createChild (parent, ...context) {
  const newChild = logger.child(...context)
  newChild.tags = { ...logger.tags }
  newChild.addTag = function (key, value) {
    newChild.tags[key] = value
  }
  return newChild
}

logger.addTag('foo', 1)
const child = createChild(logger, {})
child.addTag('bar', 2)
logger.info('this will only have `foo: 1`')
child.info('this will have both `foo: 1` and `bar: 2`')
logger.info('this will still only have `foo: 1`')
```
@mmarchini
Copy link
Contributor Author

done!

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina mcollina merged commit 962bc2b into pinojs:master May 5, 2023
17 of 19 checks passed
Copy link

github-actions bot commented May 6, 2024

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants