Skip to content

Commit 2583a65

Browse files
authored
feat: Added remove to multistream. (#2257)
Signed-off-by: Paolo Insogna <[email protected]>
1 parent 5fa2029 commit 2583a65

File tree

3 files changed

+79
-4
lines changed

3 files changed

+79
-4
lines changed

docs/api.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,14 +1461,24 @@ Exposes the Pino package version. Also available on the logger instance.
14611461
- Returns: [MultiStreamRes](#multistreamres)
14621462
14631463
Add `dest` stream to the array of streams of the current instance.
1464-
* `flushSync()`
1465-
- Returns: `undefined`
1464+
* `flushSync()`
1465+
- Returns: `undefined`
14661466
14671467
Call `flushSync` on each stream held by the current instance.
1468+
1469+
* `lastId`
1470+
- number
1471+
1472+
The ID assigned to the last stream assigned to the current instance.
14681473
* `minLevel`
14691474
- number
14701475
14711476
The minimum level amongst all the streams held by the current instance.
1477+
1478+
* `remove(id)`
1479+
- `id` [number]
1480+
1481+
Removes a stream from the array of streams of the current instance using its assigned ID.
14721482
* `streams`
14731483
- Returns: [StreamEntry[]](#streamentry)
14741484

lib/multistream.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const { DEFAULT_LEVELS } = require('./constants')
66
const DEFAULT_INFO_LEVEL = DEFAULT_LEVELS.info
77

88
function multistream (streamsArray, opts) {
9-
let counter = 0
109
streamsArray = streamsArray || []
1110
opts = opts || { dedupe: false }
1211

@@ -21,10 +20,12 @@ function multistream (streamsArray, opts) {
2120
const res = {
2221
write,
2322
add,
23+
remove,
2424
emit,
2525
flushSync,
2626
end,
2727
minLevel: 0,
28+
lastId: 0,
2829
streams: [],
2930
clone,
3031
[metadata]: true,
@@ -126,7 +127,7 @@ function multistream (streamsArray, opts) {
126127
stream: stream_,
127128
level,
128129
levelVal: undefined,
129-
id: counter++
130+
id: ++res.lastId
130131
}
131132

132133
streams.unshift(dest_)
@@ -137,6 +138,19 @@ function multistream (streamsArray, opts) {
137138
return res
138139
}
139140

141+
function remove (id) {
142+
const { streams } = this
143+
const index = streams.findIndex(s => s.id === id)
144+
145+
if (index >= 0) {
146+
streams.splice(index, 1)
147+
streams.sort(compareByLevel)
148+
this.minLevel = streams.length > 0 ? streams[0].level : -1
149+
}
150+
151+
return res
152+
}
153+
140154
function end () {
141155
for (const { stream } of this.streams) {
142156
if (typeof stream.flushSync === 'function') {
@@ -159,6 +173,7 @@ function multistream (streamsArray, opts) {
159173
return {
160174
write,
161175
add,
176+
remove,
162177
minLevel: level,
163178
streams,
164179
clone,

test/multistream.test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,56 @@ test('add a stream', function (t) {
570570
t.end()
571571
})
572572

573+
test('remove a stream', function (t) {
574+
let messageCount1 = 0
575+
let messageCount2 = 0
576+
let messageCount3 = 0
577+
578+
const stream1 = writeStream(function (data, enc, cb) {
579+
messageCount1 += 1
580+
cb()
581+
})
582+
583+
const stream2 = writeStream(function (data, enc, cb) {
584+
messageCount2 += 1
585+
cb()
586+
})
587+
588+
const stream3 = writeStream(function (data, enc, cb) {
589+
messageCount3 += 1
590+
cb()
591+
})
592+
593+
const multi = multistream()
594+
const log = pino({ level: 'trace', sync: true }, multi)
595+
596+
multi.add(stream1)
597+
const id1 = multi.lastId
598+
599+
multi.add(stream2)
600+
const id2 = multi.lastId
601+
602+
multi.add(stream3)
603+
const id3 = multi.lastId
604+
605+
log.info('line')
606+
multi.remove(id1)
607+
608+
log.info('line')
609+
multi.remove(id2)
610+
611+
log.info('line')
612+
multi.remove(id3)
613+
614+
log.info('line')
615+
multi.remove(Math.floor(Math.random() * 1000)) // non-existing id
616+
617+
t.equal(messageCount1, 1)
618+
t.equal(messageCount2, 2)
619+
t.equal(messageCount3, 3)
620+
t.end()
621+
})
622+
573623
test('multistream.add throws if not a stream', function (t) {
574624
try {
575625
pino({

0 commit comments

Comments
 (0)