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

fix: Run parent bindings formatter when creating child logger #1367

Merged
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
7 changes: 6 additions & 1 deletion lib/proto.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,12 @@ function child (bindings, options) {
} else {
instance[formattersSym] = buildFormatters(
formatters.level,
resetChildingsFormatter,
formatters.bindings
? (bindings) => ({
...formatters.bindings(JSON.parse('{' + this[chindingsSym].substr(1) + '}')),
...bindings
})
: resetChildingsFormatter,
formatters.log
)
}
Expand Down
73 changes: 73 additions & 0 deletions test/formatters.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,79 @@ test('Formatters in child logger', async ({ match }) => {
})
})

test('Parent bindings in child logger', async ({ match }) => {
const stream = sink()

const logger = pino({
formatters: {
bindings (bindings) {
return {
...bindings,
process: {
pid: bindings.pid
},
from: 'parent'
}
}
}
}, stream)

const child = logger.child({
foo: 'bar'
})

const childOut = once(stream, 'data')
child.info('hello world')

match(await childOut, {
process: {
pid: process.pid
},
from: 'parent',
foo: 'bar'
})
})

test('Parent bindings in child logger with it\'s own bindings', async ({ match }) => {
const stream = sink()
const logger = pino({
formatters: {
bindings (bindings) {
return {
process: {
pid: bindings.pid
},
from: 'parent'
}
}
}
}, stream)

const childWithBindings = logger.child({
foo: 'bar'
}, {
formatters: {
bindings (bindings) {
return {
...bindings,
from: 'child'
}
}
}
})

const childWithBindingsOut = once(stream, 'data')
childWithBindings.info('hello world')

match(await childWithBindingsOut, {
process: {
pid: process.pid
},
foo: 'bar',
from: 'child'
})
})

test('Formatters without bindings in child logger', async ({ match }) => {
const stream = sink()
const logger = pino({
Expand Down