Skip to content

Commit

Permalink
fix: Run parent bindings formatter when creating child logger (#1367)
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermelimak committed Mar 15, 2022
1 parent 6fbd2fb commit 0d6eda8
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
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

0 comments on commit 0d6eda8

Please sign in to comment.