From 0d6eda8ebec264e778da9a2a18152d8de90b958f Mon Sep 17 00:00:00 2001 From: Guilherme Kammsetzer <6863574+guilhermelimak@users.noreply.github.com> Date: Tue, 15 Mar 2022 08:15:56 -0300 Subject: [PATCH] fix: Run parent bindings formatter when creating child logger (#1367) --- lib/proto.js | 7 +++- test/formatters.test.js | 73 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/lib/proto.js b/lib/proto.js index abbeef8e3..163f848a1 100644 --- a/lib/proto.js +++ b/lib/proto.js @@ -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 ) } diff --git a/test/formatters.test.js b/test/formatters.test.js index 75d05cdb0..3fbd31e1c 100644 --- a/test/formatters.test.js +++ b/test/formatters.test.js @@ -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({