From 8ab859526478a38cbc1dde7df9388722dc388272 Mon Sep 17 00:00:00 2001 From: Nikolay Lapshin <39495311+nlapshin@users.noreply.github.com> Date: Fri, 28 Feb 2020 21:04:50 +0300 Subject: [PATCH] Fixed stream metadata loss during prettification (#780) * 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 --- lib/tools.js | 13 +++++++++++++ test/pretty.test.js | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/lib/tools.js b/lib/tools.js index 02127ffb7..fe89a0bd1 100644 --- a/lib/tools.js +++ b/lib/tools.js @@ -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', @@ -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) } } @@ -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, diff --git a/test/pretty.test.js b/test/pretty.test.js index caf9f6279..4017d0cd2 100644 --- a/test/pretty.test.js +++ b/test/pretty.test.js @@ -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') +})