-
Notifications
You must be signed in to change notification settings - Fork 892
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: pass logger/child logger as param to mixin
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`') ```
- Loading branch information
Showing
3 changed files
with
93 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters