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

call onChild when instanciating new child browser logger #2001

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ function pino (opts) {
formatters: opts.browser.formatters,
levels,
timestamp: getTimeFunction(opts),
messageKey: opts.messageKey || 'msg'
messageKey: opts.messageKey || 'msg',
onChild: opts.onChild || noop
}
logger.levels = getLevels(opts)
logger.level = level
Expand All @@ -127,7 +128,7 @@ function pino (opts) {
logger.serializers = serializers
logger._serialize = serialize
logger._stdErrSerialize = stdErrSerialize
logger.child = child
logger.child = function (...args) { return child.call(this, setOpts, ...args) }

if (transmit) logger._logEvent = createLogEventShape()

Expand Down Expand Up @@ -156,7 +157,7 @@ function pino (opts) {
})
}

function child (bindings, childOptions) {
function child (setOpts, bindings, childOptions) {
if (!bindings) {
throw new Error('missing bindings for child Pino')
}
Expand Down Expand Up @@ -194,8 +195,10 @@ function pino (opts) {

// must happen before the level is assigned
appendChildLogger(this, newLogger)
newLogger.child = function (...args) { return child.call(this, setOpts, ...args) }
// required to actually initialize the logger functions for any given child
newLogger.level = childOptions.level || this.level // allow level to be set by childOptions
setOpts.onChild(newLogger)

return newLogger
}
Expand Down
14 changes: 14 additions & 0 deletions test/browser-child.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,20 @@ test('changing child log level should not affect parent log behavior', ({ end, s
end()
})

test('onChild callback should be called when new child is created', ({ end, pass, plan }) => {
plan(1)
const instance = pino({
level: 'error',
browser: {},
onChild: (_child) => {
pass('onChild callback was called')
end()
}
})

instance.child({})
})

function checkLogObjects (is, same, actual, expected) {
is(actual.time <= Date.now(), true, 'time is greater than Date.now()')

Expand Down