Skip to content

Commit

Permalink
Makes #711 mergable (#777)
Browse files Browse the repository at this point in the history
* Log literal null values (fixes #706)

* When passing multiple parameters, omit null value

Co-authored-by: Nik <[email protected]>
  • Loading branch information
jsumners and NikxDa authored Feb 24, 2020
1 parent d0f953b commit 0e47e2d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
1 change: 1 addition & 0 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
19 changes: 12 additions & 7 deletions lib/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand Down
30 changes: 30 additions & 0 deletions test/levels.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 0e47e2d

Please sign in to comment.