diff --git a/example.js b/example.js index 5e39692c0..866338e14 100644 --- a/example.js +++ b/example.js @@ -31,5 +31,6 @@ pino.trace('this is a trace statement') pino.debug('this is a "debug" statement with "') pino.info(new Error('kaboom')) +pino.info(null) pino.info(new Error('kaboom'), 'with', 'a', 'message') diff --git a/lib/tools.js b/lib/tools.js index e446c4332..16a209fcf 100644 --- a/lib/tools.js +++ b/lib/tools.js @@ -28,15 +28,20 @@ function noop () {} function genLog (z) { return function LOG (o, ...n) { - if (typeof o === 'object' && o !== null) { - if (o.method && o.headers && o.socket) { - o = mapHttpRequest(o) - } else if (typeof o.setHeader === 'function') { - o = mapHttpResponse(o) + if (typeof o === 'object') { + if (o !== null) { + if (o.method && o.headers && o.socket) { + o = mapHttpRequest(o) + } else if (typeof o.setHeader === 'function') { + o = mapHttpResponse(o) + } } if (this[nestedKeySym]) o = { [this[nestedKeySym]]: o } - this[writeSym](o, format(null, n, this[formatOptsSym]), z) - } else this[writeSym](null, format(o, n, this[formatOptsSym]), z) + const formatParams = (o === null && n.length === 0) ? [null] : n + this[writeSym](o, format(null, formatParams, this[formatOptsSym]), z) + } else { + this[writeSym](null, format(o, n, this[formatOptsSym]), z) + } } } diff --git a/test/levels.test.js b/test/levels.test.js index b7ac1cc80..06fea3e1b 100644 --- a/test/levels.test.js +++ b/test/levels.test.js @@ -403,6 +403,36 @@ test('passes when creating a default value that exists in logger levels', async }) }) +test('log null value when message is null', async ({ is }) => { + const expected = { + msg: null, + level: 30 + } + + const stream = sink() + const instance = pino(stream) + instance.level = 'info' + instance.info(null) + + const result = await once(stream, 'data') + check(is, result, expected.level, expected.msg) +}) + +test('concatenate multiple params when base param is null', async ({ is }) => { + const expected = { + msg: 'a string', + level: 30 + } + + const stream = sink() + const instance = pino(stream) + instance.level = 'info' + instance.info(null, 'a', 'string') + + const result = await once(stream, 'data') + check(is, result, expected.level, expected.msg) +}) + test('fatal method sync-flushes the destination if sync flushing is available', async ({ pass, doesNotThrow, plan }) => { plan(2) const stream = sink()