Skip to content

Commit

Permalink
fix: Throw when using custom level formatters with multiple transports (
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermelimak committed Mar 8, 2022
1 parent e0d0f0d commit 26c2d43
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ Changes the shape of the log level. The default shape is `{ level: number }`.
The function takes two arguments, the label of the level (e.g. `'info'`)
and the numeric value (e.g. `30`).

ps: The log level cannot be customized when using multiple transports

```js
const formatters = {
level (label, number) {
Expand Down
3 changes: 3 additions & 0 deletions lib/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,9 @@ function createArgsNormalizer (defaultOptions) {
if (opts.transport instanceof SonicBoom || opts.transport.writable || opts.transport._writableState) {
throw Error('option.transport do not allow stream, please pass to option directly. e.g. pino(transport)')
}
if (opts.transport.targets && opts.transport.targets.length && opts.formatters && typeof opts.formatters.level === 'function') {
throw Error('option.transport.targets do not allow custom level formatters')
}
stream = transport({ caller, ...opts.transport })
}
opts = Object.assign({}, defaultOptions, opts)
Expand Down
27 changes: 27 additions & 0 deletions test/formatters.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,30 @@ test('formatter with transport', async ({ match, equal }) => {
nested: { object: true }
})
})

test('throws when custom level formatter is used with transport.targets', async ({ throws }) => {
const destination = join(
os.tmpdir(),
'_' + Math.random().toString(36).substr(2, 9)
)

throws(() => {
pino({
formatters: {
level (label) {
return label
}
},
transport: {
targets: [
{
target: 'pino/file',
options: { destination }
}
]
}
}
)
},
Error('option.transport.targets do not allow custom level formatters'))
})

0 comments on commit 26c2d43

Please sign in to comment.