From e6d355cd77adb0e44ca06faf5f49ae2f39acdd4a Mon Sep 17 00:00:00 2001 From: sun0day Date: Tue, 27 Sep 2022 18:25:17 +0800 Subject: [PATCH] fix(tools): log[level] compatible with when first argument is undefined (fix #1555) (#1565) --- lib/tools.js | 2 +- test/basic.test.js | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/tools.js b/lib/tools.js index 674198b43..b44a96129 100644 --- a/lib/tools.js +++ b/lib/tools.js @@ -53,7 +53,7 @@ function genLog (level, hook) { } this[writeSym](o, format(msg, formatParams, this[formatOptsSym]), level) } else { - this[writeSym](null, format(o, n, this[formatOptsSym]), level) + this[writeSym](null, format(o === undefined ? n.shift() : o, n, this[formatOptsSym]), level) } } } diff --git a/test/basic.test.js b/test/basic.test.js index ca4ca1e97..e8310d689 100644 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -109,6 +109,22 @@ function levelTest (name, level) { same(Object.keys(obj), ['hello']) }) + test(`passing a undefined and a string at level ${name}`, async ({ equal, same }) => { + const stream = sink() + const instance = pino(stream) + instance.level = name + instance[name](undefined, 'a string') + const result = await once(stream, 'data') + equal(new Date(result.time) <= new Date(), true, 'time is greater than Date.now()') + delete result.time + same(result, { + pid, + hostname, + level, + msg: 'a string' + }) + }) + test(`overriding object key by string at level ${name}`, async ({ equal, same }) => { const stream = sink() const instance = pino(stream)