Skip to content

Commit

Permalink
call onChild when instanciating new child browser logger
Browse files Browse the repository at this point in the history
  • Loading branch information
hollykurt committed Jul 4, 2024
1 parent a41b4d3 commit 0a0f2fa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
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

0 comments on commit 0a0f2fa

Please sign in to comment.