Skip to content

Commit

Permalink
Add default levels to opts in multistream (#1760)
Browse files Browse the repository at this point in the history
* Add default levels to opts in multistream

* moving levels to function

---------

Co-authored-by: XGA745 <tzviki.wolvovsky@capitalone.com>
  • Loading branch information
tzviki and XGA745 authored Aug 4, 2023
1 parent a47c857 commit 459404f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
17 changes: 9 additions & 8 deletions lib/multistream.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
const metadata = Symbol.for('pino.metadata')
const { levels } = require('./levels')

const defaultLevels = Object.create(levels)
defaultLevels.silent = Infinity

const DEFAULT_INFO_LEVEL = levels.info

function multistream (streamsArray, opts) {
let counter = 0
streamsArray = streamsArray || []
opts = opts || { dedupe: false }

let levels = defaultLevels
const streamLevels = Object.create(levels)
streamLevels.silent = Infinity
if (opts.levels && typeof opts.levels === 'object') {
levels = opts.levels
Object.keys(opts.levels).forEach(i => {
streamLevels[i] = opts.levels[i]
})
}

const res = {
Expand All @@ -26,7 +26,8 @@ function multistream (streamsArray, opts) {
minLevel: 0,
streams: [],
clone,
[metadata]: true
[metadata]: true,
streamLevels
}

if (Array.isArray(streamsArray)) {
Expand Down Expand Up @@ -99,13 +100,13 @@ function multistream (streamsArray, opts) {
throw Error('stream object needs to implement either StreamEntry or DestinationStream interface')
}

const { streams } = this
const { streams, streamLevels } = this

let level
if (typeof dest.levelVal === 'number') {
level = dest.levelVal
} else if (typeof dest.level === 'string') {
level = levels[dest.level]
level = streamLevels[dest.level]
} else if (typeof dest.level === 'number') {
level = dest.level
} else {
Expand Down
24 changes: 24 additions & 0 deletions test/transport/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,30 @@ test('pino.transport with two files and custom levels', async ({ same, teardown
})
})

test('pino.transport without specifying default levels', async ({ same, teardown }) => {
const dest = file()
const transport = pino.transport({
targets: [{
level: 'foo',
target: join(__dirname, '..', 'fixtures', 'to-file-transport.js'),
options: { destination: dest }
}],
levels: { foo: 25 }
})
teardown(transport.end.bind(transport))
const instance = pino(transport)
instance.info('hello')
await Promise.all([watchFileCreated(dest)])
const result1 = JSON.parse(await readFile(dest))
delete result1.time
same(result1, {
pid,
hostname,
level: 30,
msg: 'hello'
})
})

test('pino.transport with two files and dedupe', async ({ same, teardown }) => {
const dest1 = file()
const dest2 = file()
Expand Down

0 comments on commit 459404f

Please sign in to comment.