Skip to content

Commit 0e47e2d

Browse files
jsumnersNikxDa
andauthored
Makes pinojs#711 mergable (pinojs#777)
* Log literal null values (fixes pinojs#706) * When passing multiple parameters, omit null value Co-authored-by: Nik <[email protected]>
1 parent d0f953b commit 0e47e2d

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

example.js

+1
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ pino.trace('this is a trace statement')
3131
pino.debug('this is a "debug" statement with "')
3232

3333
pino.info(new Error('kaboom'))
34+
pino.info(null)
3435

3536
pino.info(new Error('kaboom'), 'with', 'a', 'message')

lib/tools.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,20 @@ function noop () {}
2828

2929
function genLog (z) {
3030
return function LOG (o, ...n) {
31-
if (typeof o === 'object' && o !== null) {
32-
if (o.method && o.headers && o.socket) {
33-
o = mapHttpRequest(o)
34-
} else if (typeof o.setHeader === 'function') {
35-
o = mapHttpResponse(o)
31+
if (typeof o === 'object') {
32+
if (o !== null) {
33+
if (o.method && o.headers && o.socket) {
34+
o = mapHttpRequest(o)
35+
} else if (typeof o.setHeader === 'function') {
36+
o = mapHttpResponse(o)
37+
}
3638
}
3739
if (this[nestedKeySym]) o = { [this[nestedKeySym]]: o }
38-
this[writeSym](o, format(null, n, this[formatOptsSym]), z)
39-
} else this[writeSym](null, format(o, n, this[formatOptsSym]), z)
40+
const formatParams = (o === null && n.length === 0) ? [null] : n
41+
this[writeSym](o, format(null, formatParams, this[formatOptsSym]), z)
42+
} else {
43+
this[writeSym](null, format(o, n, this[formatOptsSym]), z)
44+
}
4045
}
4146
}
4247

test/levels.test.js

+30
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,36 @@ test('passes when creating a default value that exists in logger levels', async
403403
})
404404
})
405405

406+
test('log null value when message is null', async ({ is }) => {
407+
const expected = {
408+
msg: null,
409+
level: 30
410+
}
411+
412+
const stream = sink()
413+
const instance = pino(stream)
414+
instance.level = 'info'
415+
instance.info(null)
416+
417+
const result = await once(stream, 'data')
418+
check(is, result, expected.level, expected.msg)
419+
})
420+
421+
test('concatenate multiple params when base param is null', async ({ is }) => {
422+
const expected = {
423+
msg: 'a string',
424+
level: 30
425+
}
426+
427+
const stream = sink()
428+
const instance = pino(stream)
429+
instance.level = 'info'
430+
instance.info(null, 'a', 'string')
431+
432+
const result = await once(stream, 'data')
433+
check(is, result, expected.level, expected.msg)
434+
})
435+
406436
test('fatal method sync-flushes the destination if sync flushing is available', async ({ pass, doesNotThrow, plan }) => {
407437
plan(2)
408438
const stream = sink()

0 commit comments

Comments
 (0)