Skip to content

Commit

Permalink
Fixed stream metadata loss during prettification (#780)
Browse files Browse the repository at this point in the history
* Fixed stream metadata loss during prettification

For streams with Symbol('metadata') true flag, pino metadata was lost during prettification

* Fixed way to adding metadata in stream

* Created setMetadataProps function which adding logger metadata to stream instance if need
  • Loading branch information
nlapshin committed Feb 28, 2020
1 parent d237668 commit 8ab8595
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/tools.js
Expand Up @@ -178,6 +178,7 @@ function prettifierMetaWrapper (pretty, dest) {
return
}
warned = true
setMetadataProps(dest, this)
dest.write(pretty(Object.assign({
level: 40, // warn
msg: 'pino.final with prettyPrint does not support flushing',
Expand Down Expand Up @@ -237,6 +238,8 @@ function prettifierMetaWrapper (pretty, dest) {

const formatted = pretty(typeof redact === 'function' ? redact(obj) : obj)
if (formatted === undefined) return

setMetadataProps(dest, this)
dest.write(formatted)
}
}
Expand Down Expand Up @@ -351,6 +354,16 @@ function stringify (obj) {
}
}

function setMetadataProps (dest, that) {
if (dest[needsMetadataGsym] === true) {
dest.lastLevel = that.lastLevel
dest.lastMsg = that.lastMsg
dest.lastObj = that.lastObj
dest.lastTime = that.lastTime
dest.lastLogger = that.lastLogger
}
}

module.exports = {
noop,
buildSafeSonicBoom,
Expand Down
38 changes: 38 additions & 0 deletions test/pretty.test.js
Expand Up @@ -272,3 +272,41 @@ test('works as expected with an object with the msg prop', async ({ isNot }) =>
await once(child, 'close')
isNot(actual.match(/\(123456 on abcdefghijklmnopqr\): hello/), null)
})

test('should not lose stream metadata for streams with `needsMetadataGsym` flag', async ({ isNot }) => {
const dest = new Writable({
objectMode: true,
write () {
isNot(typeof this.lastLevel === 'undefined', true)
isNot(typeof this.lastMsg === 'undefined', true)
isNot(typeof this.lastObj === 'undefined', true)
isNot(typeof this.lastTime === 'undefined', true)
isNot(typeof this.lastLogger === 'undefined', true)
}
})

dest[pino.symbols.needsMetadataGsym] = true

const log = pino({
prettyPrint: true
}, dest)
log.info('foo')
})

test('should not add stream metadata for streams without `needsMetadataGsym` flag', async ({ is }) => {
const dest = new Writable({
objectMode: true,
write () {
is(typeof this.lastLevel === 'undefined', true)
is(typeof this.lastMsg === 'undefined', true)
is(typeof this.lastObj === 'undefined', true)
is(typeof this.lastTime === 'undefined', true)
is(typeof this.lastLogger === 'undefined', true)
}
})

const log = pino({
prettyPrint: true
}, dest)
log.info('foo')
})

0 comments on commit 8ab8595

Please sign in to comment.