From 9cc90e37b18dda7bc305b4915df17c7a78f41551 Mon Sep 17 00:00:00 2001 From: Zsombor Franczia Date: Tue, 19 Oct 2021 09:41:31 +0200 Subject: [PATCH] Fixes #1167 (dedupe should write to all streams matching the target level) (#1172) --- lib/multistream.js | 6 +----- test/multistream.test.js | 42 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/lib/multistream.js b/lib/multistream.js index 69819b0b9..96527986b 100644 --- a/lib/multistream.js +++ b/lib/multistream.js @@ -59,17 +59,13 @@ function multistream (streamsArray, opts) { stream.lastObj = lastObj stream.lastLogger = lastLogger } - if (!opts.dedupe) { + if (!opts.dedupe || dest.level === level) { stream.write(data) } } else { break } } - - if (opts.dedupe && stream) { - stream.write(data) - } } function flushSync () { diff --git a/test/multistream.test.js b/test/multistream.test.js index ff363d3cb..94ae848bf 100644 --- a/test/multistream.test.js +++ b/test/multistream.test.js @@ -429,6 +429,48 @@ test('dedupe', function (t) { t.end() }) +test('dedupe when some streams has the same level', function (t) { + let messageCount = 0 + const stream1 = writeStream(function (data, enc, cb) { + messageCount -= 1 + cb() + }) + + const stream2 = writeStream(function (data, enc, cb) { + messageCount += 1 + cb() + }) + + const stream3 = writeStream(function (data, enc, cb) { + messageCount += 1 + cb() + }) + + const streams = [ + { + stream: stream1, + level: 'info' + }, + { + stream: stream2, + level: 'fatal' + }, + { + stream: stream3, + level: 'fatal' + } + ] + + const log = pino({ + level: 'trace' + }, multistream(streams, { dedupe: true })) + log.info('info stream') + log.fatal('fatal streams') + log.fatal('fatal streams') + t.equal(messageCount, 3) + t.end() +}) + test('no stream', function (t) { const log = pino({ level: 'trace'