From 43906c71e7bf2a09af2152cf48227eaf35f0b8ce Mon Sep 17 00:00:00 2001 From: Sameer Srivastava Date: Fri, 29 Oct 2021 18:40:35 +0530 Subject: [PATCH] Fixes issue with nested keys (#1198) * fix: fixes issue with nested keys * chore: use strictSame in nested key test --- lib/tools.js | 2 +- test/basic.test.js | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/tools.js b/lib/tools.js index 8fbcca977..f32fe53a6 100644 --- a/lib/tools.js +++ b/lib/tools.js @@ -173,7 +173,7 @@ function asJson (obj, msg, num, time) { } } - if (this[nestedKeySym]) { + if (this[nestedKeySym] && propStr) { // place all the obj properties under the specified key // the nested key is already formatted from the constructor return data + this[nestedKeyStrSym] + propStr.slice(1) + '}' + msgStr + end diff --git a/test/basic.test.js b/test/basic.test.js index 4eaa97174..6995431c2 100644 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -712,3 +712,21 @@ test('correctly log number', async (t) => { const { msg } = await once(stream, 'data') t.equal(msg, 42) }) + +test('nestedKey should not be used for non-objects', async ({ strictSame }) => { + const stream = sink() + const message = 'hello' + const nestedKey = 'stuff' + const instance = pino({ + nestedKey + }, stream) + instance.info(message) + const result = await once(stream, 'data') + delete result.time + strictSame(result, { + pid, + hostname, + level: 30, + msg: message + }) +})