From 1834a2c04b790acea2c245a99404190708812152 Mon Sep 17 00:00:00 2001 From: rjmohammad Date: Fri, 23 Jun 2023 00:59:59 -0400 Subject: [PATCH 1/5] fix: adds support for printing number and boolean messageKey value type closes: Pino Pretty does not output numbers but only strings. #433 --- index.js | 2 +- lib/utils.js | 2 +- test/basic.test.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 1690844c..000a873c 100644 --- a/index.js +++ b/index.js @@ -186,7 +186,7 @@ function prettyFactory (options) { if (singleLine) line += EOL line += prettifiedErrorLog } else if (!hideObject) { - const skipKeys = [messageKey, levelKey, timestampKey].filter(key => typeof log[key] === 'string' || typeof log[key] === 'number') + const skipKeys = [messageKey, levelKey, timestampKey].filter(key => typeof log[key] === 'string' || typeof log[key] === 'number' || typeof log[key] === 'boolean') const prettifiedObject = prettifyObject({ input: log, skipKeys, diff --git a/lib/utils.js b/lib/utils.js index 159dc510..db3536ba 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -280,7 +280,7 @@ function prettifyMessage ({ log, messageFormat, messageKey = MESSAGE_KEY, colori return colorizer.message(msg) } if (messageKey in log === false) return undefined - if (typeof log[messageKey] !== 'string') return undefined + if (typeof log[messageKey] !== 'string' && typeof log[messageKey] !== 'number' && typeof log[messageKey] !== 'boolean') return undefined return colorizer.message(log[messageKey]) } diff --git a/test/basic.test.js b/test/basic.test.js index 5dfc05f5..5cf4188b 100644 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -118,6 +118,51 @@ test('basic prettifier tests', (t) => { log.info('foo') }) + t.test('can print message key value when its a string', (t) => { + t.plan(1) + const pretty = prettyFactory() + const log = pino({}, new Writable({ + write (chunk, enc, cb) { + const formatted = pretty(chunk.toString()) + t.equal( + formatted, + `[${formattedEpoch}] INFO (${pid}): baz\n` + ) + cb() + } + })) + log.info('baz') + }) + t.test('can print message key value when its a number', (t) => { + t.plan(1) + const pretty = prettyFactory() + const log = pino({}, new Writable({ + write (chunk, enc, cb) { + const formatted = pretty(chunk.toString()) + t.equal( + formatted, + `[${formattedEpoch}] INFO (${pid}): 42\n` + ) + cb() + } + })) + log.info(42) + }) + t.test('can print message key value when its a string', (t) => { + t.plan(1) + const pretty = prettyFactory() + const log = pino({}, new Writable({ + write (chunk, enc, cb) { + const formatted = pretty(chunk.toString()) + t.equal( + formatted, + `[${formattedEpoch}] INFO (${pid}): true\n` + ) + cb() + } + })) + log.info(true) + }) t.test('can use different message keys', (t) => { t.plan(1) const pretty = prettyFactory({ messageKey: 'bar' }) From b1dbbaeaadd3381814ca72c0f47cf2661557fd7f Mon Sep 17 00:00:00 2001 From: Rana Mohammad Date: Sun, 25 Jun 2023 11:31:38 -0400 Subject: [PATCH 2/5] Update test/basic.test.js Co-authored-by: James Sumners --- test/basic.test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/basic.test.js b/test/basic.test.js index 5cf4188b..5743484e 100644 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -133,6 +133,7 @@ test('basic prettifier tests', (t) => { })) log.info('baz') }) + t.test('can print message key value when its a number', (t) => { t.plan(1) const pretty = prettyFactory() From a14142f86b4b0524c51f1dc3ce50da84c5494ba7 Mon Sep 17 00:00:00 2001 From: Rana Mohammad Date: Sun, 25 Jun 2023 11:31:45 -0400 Subject: [PATCH 3/5] Update test/basic.test.js Co-authored-by: James Sumners --- test/basic.test.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/basic.test.js b/test/basic.test.js index 5743484e..0de4976a 100644 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -149,7 +149,8 @@ test('basic prettifier tests', (t) => { })) log.info(42) }) - t.test('can print message key value when its a string', (t) => { + + t.test('can print message key value when its a boolean', (t) => { t.plan(1) const pretty = prettyFactory() const log = pino({}, new Writable({ From 425414bcb7477c342ac6e4eea1ebf072a69134ec Mon Sep 17 00:00:00 2001 From: Rana Mohammad Date: Sun, 25 Jun 2023 11:32:09 -0400 Subject: [PATCH 4/5] Update test/basic.test.js Co-authored-by: James Sumners --- test/basic.test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/basic.test.js b/test/basic.test.js index 0de4976a..8514c416 100644 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -165,6 +165,7 @@ test('basic prettifier tests', (t) => { })) log.info(true) }) + t.test('can use different message keys', (t) => { t.plan(1) const pretty = prettyFactory({ messageKey: 'bar' }) From ee1dc4042faab3599737f26cf7e5a36a2947f2a4 Mon Sep 17 00:00:00 2001 From: rjmohammad Date: Sat, 1 Jul 2023 01:14:26 -0400 Subject: [PATCH 5/5] fix: checks for undefined specifically instead of falsey values --- index.js | 2 +- test/basic.test.js | 22 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 000a873c..41eb7602 100644 --- a/index.js +++ b/index.js @@ -162,7 +162,7 @@ function prettyFactory (options) { line += ':' } - if (prettifiedMessage) { + if (prettifiedMessage !== undefined) { if (line.length > 0) { line = `${line} ${prettifiedMessage}` } else { diff --git a/test/basic.test.js b/test/basic.test.js index 8514c416..9986811c 100644 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -133,7 +133,7 @@ test('basic prettifier tests', (t) => { })) log.info('baz') }) - + t.test('can print message key value when its a number', (t) => { t.plan(1) const pretty = prettyFactory() @@ -149,7 +149,23 @@ test('basic prettifier tests', (t) => { })) log.info(42) }) - + + t.test('can print message key value when its a Number(0)', (t) => { + t.plan(1) + const pretty = prettyFactory() + const log = pino({}, new Writable({ + write (chunk, enc, cb) { + const formatted = pretty(chunk.toString()) + t.equal( + formatted, + `[${formattedEpoch}] INFO (${pid}): 0\n` + ) + cb() + } + })) + log.info(0) + }) + t.test('can print message key value when its a boolean', (t) => { t.plan(1) const pretty = prettyFactory() @@ -165,7 +181,7 @@ test('basic prettifier tests', (t) => { })) log.info(true) }) - + t.test('can use different message keys', (t) => { t.plan(1) const pretty = prettyFactory({ messageKey: 'bar' })