From 3e8870699de32e2c4073b2b11ca3dc636002412d Mon Sep 17 00:00:00 2001 From: Paavo Bennett Date: Mon, 4 Nov 2024 17:52:41 -0600 Subject: [PATCH] fix: unescape \\ for levelKey closes #456 --- lib/pretty.js | 12 +++++++----- test/basic.test.js | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/pretty.js b/lib/pretty.js index 5284d7bc..a2b6813e 100644 --- a/lib/pretty.js +++ b/lib/pretty.js @@ -147,11 +147,13 @@ function pretty (inputData) { this.messageKey, this.levelKey, this.timestampKey - ].filter(key => { - return typeof log[key] === 'string' || - typeof log[key] === 'number' || - typeof log[key] === 'boolean' - }) + ] + .map((key) => key.replaceAll(/\\/g, '')) + .filter(key => { + return typeof log[key] === 'string' || + typeof log[key] === 'number' || + typeof log[key] === 'boolean' + }) const prettifiedObject = prettifyObject({ log, skipKeys, diff --git a/test/basic.test.js b/test/basic.test.js index a62abbf3..3437d1e4 100644 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -218,6 +218,22 @@ test('basic prettifier tests', (t) => { log.info({ msg: 'foo', bar: 'warn' }) }) + t.test('can use nested level keys', (t) => { + t.plan(1) + const pretty = prettyFactory({ levelKey: 'log\\.level' }) + const log = pino({}, new Writable({ + write (chunk, enc, cb) { + const formatted = pretty(chunk.toString()) + t.equal( + formatted, + `[${formattedEpoch}] WARN (${pid}): foo\n` + ) + cb() + } + })) + log.info({ msg: 'foo', 'log.level': 'warn' }) + }) + t.test('can use a customPrettifier on default level output', (t) => { t.plan(1) const veryCustomLevels = {