From 2b9eee7178565fd0f6547d9d2bc334066c55b8e1 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Tue, 29 Nov 2016 14:05:16 -0600 Subject: [PATCH] Fix #139. --- pino.js | 4 ++-- test/basic.test.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/pino.js b/pino.js index f4181b7b0..b6aedc87e 100644 --- a/pino.js +++ b/pino.js @@ -406,8 +406,8 @@ function genLog (z) { } p = n.length = arguments.length - l if (p > 1) { - l = countInterp(a, '%j') - if (l && typeof a === 'string') { + l = typeof a === 'string' ? countInterp(a, '%j') : 0 + if (l) { n.length = l + countInterp(a, '%d') + countInterp(a, '%s') + 1 o = `${util.format.apply(null, n)}` } else { diff --git a/test/basic.test.js b/test/basic.test.js index 350297150..b1cea58b8 100644 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -292,6 +292,42 @@ test('correctly escape msg strings', function (t) { instance.fatal('this contains "') }) +// https://github.com/pinojs/pino/issues/139 +test('object and format string', function (t) { + var instance = pino(sink(function (chunk, enc, cb) { + delete chunk.time + t.deepEqual(chunk, { + pid: pid, + hostname: hostname, + level: 30, + msg: 'foo bar', + v: 1 + }) + t.end() + cb() + })) + + instance.info({}, 'foo %s', 'bar') +}) + +test('object and format string property', function (t) { + var instance = pino(sink(function (chunk, enc, cb) { + delete chunk.time + t.deepEqual(chunk, { + pid: pid, + hostname: hostname, + level: 30, + msg: 'foo bar', + answer: 42, + v: 1 + }) + t.end() + cb() + })) + + instance.info({ answer: 42 }, 'foo %s', 'bar') +}) + test('correctly strip undefined when returned from toJSON', function (t) { t.plan(1)