Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,10 @@ function transport (fullOptions) {
})
})

usesMultistream = options.targets.length + options.pipelines.length > 1
const targetsHaveLevel = options.targets.some(t => t.level != null)
const pipelinesHaveLevel = options.pipelines.some(p => p.some(t => t.level != null))
usesMultistream = options.targets.length + options.pipelines.length > 1 ||
targetsHaveLevel || pipelinesHaveLevel
} else if (pipeline) {
target = bundlerOverrides['pino-worker'] || join(__dirname, 'worker.js')
options.pipelines = [pipeline.map((dest) => {
Expand Down
5 changes: 4 additions & 1 deletion lib/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ module.exports = async function ({ targets, pipelines, levels, dedupe }) {
// OR
//
// pino.transport({ pipeline: ... })
if (targetStreams.length === 1) {
//
// When a `level` is set on the single target/pipeline, fall through to the
// multistream path so the per-target level filter is honored (#1996).
if (targetStreams.length === 1 && targetStreams[0].level == null) {
return targetStreams[0].stream
} else {
return build(process, {
Expand Down
53 changes: 53 additions & 0 deletions test/transport/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,59 @@ test('pino.transport with two files and dedupe', async (t) => {
})
})

test('pino.transport with single target in targets array honors level filter (#1996)', async (t) => {
const destination = file()
const transport = pino.transport({
targets: [{
level: 'error',
target: join(__dirname, '..', 'fixtures', 'to-file-transport.js'),
options: { destination }
}]
})
t.after(transport.end.bind(transport))
const instance = pino({ level: 'debug' }, transport)
instance.debug('debug-msg')
instance.info('info-msg')
instance.warn('warn-msg')
instance.error('error-msg')
await watchForWrite(destination, 'error-msg')
const lines = (await readFile(destination, 'utf8')).trim().split('\n').filter(Boolean)
assert.equal(lines.length, 1, 'only error should be written to destination')
const result = JSON.parse(lines[0])
delete result.time
assert.deepEqual(result, {
pid,
hostname,
level: 50,
msg: 'error-msg'
})
})

test('pino.transport with single target in targets array without level keeps logger.level gating', async (t) => {
const destination = file()
const transport = pino.transport({
targets: [{
target: join(__dirname, '..', 'fixtures', 'to-file-transport.js'),
options: { destination }
}]
})
t.after(transport.end.bind(transport))
const instance = pino({ level: 'debug' }, transport)
instance.debug('debug-msg')
instance.info('info-msg')
await watchForWrite(destination, 'info-msg')
const lines = (await readFile(destination, 'utf8')).trim().split('\n').filter(Boolean)
assert.equal(lines.length, 2, 'both debug and info reach the single target when no per-target level is set')
const first = JSON.parse(lines[0])
delete first.time
assert.deepEqual(first, {
pid,
hostname,
level: 20,
msg: 'debug-msg'
})
})

test('pino.transport with an array including a pino-pretty destination', async (t) => {
const dest1 = file()
const dest2 = file()
Expand Down
Loading