Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed stream metadata loss during prettification #780

Merged
merged 3 commits into from
Feb 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/tools.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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')
})