Skip to content

Commit 5dd282c

Browse files
authored
feat: add dedupe option to transport (#1640)
* feat: add dedupe option to transport * docs: add transport dedupe info
1 parent af93e8e commit 5dd282c

File tree

7 files changed

+66
-3
lines changed

7 files changed

+66
-3
lines changed

docs/api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,7 @@ For more on transports, how they work, and how to create them see the [`Transpor
12001200
* `worker`: [Worker thread](https://nodejs.org/api/worker_threads.html#worker_threads_new_worker_filename_options) configuration options. Additionally, the `worker` option supports `worker.autoEnd`. If this is set to `false` logs will not be flushed on process exit. It is then up to the developer to call `transport.end()` to flush logs.
12011201
* `targets`: May be specified instead of `target`. Must be an array of transport configurations. Transport configurations include the aforementioned `options` and `target` options plus a `level` option which will send only logs above a specified level to a transport.
12021202
* `pipeline`: May be specified instead of `target`. Must be an array of transport configurations. Transport configurations include the aforementioned `options` and `target` options. All intermediate steps in the pipeline _must_ be `Transform` streams and not `Writable`.
1203+
* `dedupe`: See [pino.multistream options](#pino-multistream)
12031204
12041205
<a id="pino-multistream"></a>
12051206

docs/transports.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,19 @@ const transport = pino.transport({
111111
pino(transport)
112112
```
113113
114+
It is also possible to use the `dedupe` option to send logs only to the stream with the higher level.
115+
```js
116+
const pino = require('pino')
117+
const transport = pino.transport({
118+
targets: [
119+
{ target: '/absolute/path/to/my-transport.mjs', level: 'error' },
120+
{ target: 'some-file-transport', options: { destination: '/dev/null' }
121+
],
122+
dedupe: true
123+
})
124+
pino(transport)
125+
```
126+
114127
For more details on `pino.transport` see the [API docs for `pino.transport`][pino-transport].
115128
116129
[pino-transport]: /docs/api.md#pino-transport

lib/transport.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function flush (stream) {
7171
}
7272

7373
function transport (fullOptions) {
74-
const { pipeline, targets, levels, options = {}, worker = {}, caller = getCallers() } = fullOptions
74+
const { pipeline, targets, levels, dedupe, options = {}, worker = {}, caller = getCallers() } = fullOptions
7575

7676
// Backwards compatibility
7777
const callers = typeof caller === 'string' ? [caller] : caller
@@ -107,6 +107,10 @@ function transport (fullOptions) {
107107
options.levels = levels
108108
}
109109

110+
if (dedupe) {
111+
options.dedupe = dedupe
112+
}
113+
110114
return buildStream(fixTarget(target), options, worker)
111115

112116
function fixTarget (origin) {

lib/worker.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const loadTransportStreamBuilder = require('./transport-stream')
99

1010
/* istanbul ignore file */
1111

12-
module.exports = async function ({ targets, levels }) {
12+
module.exports = async function ({ targets, levels, dedupe }) {
1313
targets = await Promise.all(targets.map(async (t) => {
1414
const fn = await loadTransportStreamBuilder(t.target)
1515
const stream = await fn(t.options)
@@ -38,7 +38,7 @@ module.exports = async function ({ targets, levels }) {
3838
})
3939

4040
function process (stream) {
41-
const multi = pino.multistream(targets, { levels })
41+
const multi = pino.multistream(targets, { levels, dedupe })
4242
// TODO manage backpressure
4343
stream.on('data', function (chunk) {
4444
const { lastTime, lastMsg, lastObj, lastLevel } = this

pino.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ declare namespace pino {
262262
interface TransportMultiOptions<TransportOptions = Record<string, any>> extends TransportBaseOptions<TransportOptions>{
263263
targets: readonly TransportTargetOptions<TransportOptions>[],
264264
levels?: Record<string, number>
265+
dedupe?: boolean
265266
}
266267

267268
interface MultiStreamOptions {

test/transport/core.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,44 @@ test('pino.transport with two files and custom levels', async ({ same, teardown
201201
})
202202
})
203203

204+
test('pino.transport with two files and dedupe', async ({ same, teardown }) => {
205+
const dest1 = file()
206+
const dest2 = file()
207+
const transport = pino.transport({
208+
dedupe: true,
209+
targets: [{
210+
level: 'info',
211+
target: join(__dirname, '..', 'fixtures', 'to-file-transport.js'),
212+
options: { destination: dest1 }
213+
}, {
214+
level: 'error',
215+
target: join(__dirname, '..', 'fixtures', 'to-file-transport.js'),
216+
options: { destination: dest2 }
217+
}]
218+
})
219+
teardown(transport.end.bind(transport))
220+
const instance = pino(transport)
221+
instance.info('hello')
222+
instance.error('world')
223+
await Promise.all([watchFileCreated(dest1), watchFileCreated(dest2)])
224+
const result1 = JSON.parse(await readFile(dest1))
225+
delete result1.time
226+
same(result1, {
227+
pid,
228+
hostname,
229+
level: 30,
230+
msg: 'hello'
231+
})
232+
const result2 = JSON.parse(await readFile(dest2))
233+
delete result2.time
234+
same(result2, {
235+
pid,
236+
hostname,
237+
level: 50,
238+
msg: 'world'
239+
})
240+
})
241+
204242
test('pino.transport with an array including a pino-pretty destination', async ({ same, match, teardown }) => {
205243
const dest1 = file()
206244
const dest2 = file()

test/types/pino-transport.test-d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,9 @@ pino.transport({
120120
},
121121
options: { id: 'abc' }
122122
})
123+
124+
// Dedupe
125+
pino.transport({
126+
targets: [],
127+
dedupe: true,
128+
})

0 commit comments

Comments
 (0)