Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unescape \\ for levelKey #538

Merged
merged 2 commits into from
Nov 8, 2024
Merged
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
12 changes: 7 additions & 5 deletions lib/pretty.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,13 @@ function pretty (inputData) {
this.messageKey,
this.levelKey,
this.timestampKey
].filter(key => {
return typeof log[key] === 'string' ||
typeof log[key] === 'number' ||
typeof log[key] === 'boolean'
})
]
.map((key) => key.replaceAll(/\\/g, ''))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This strips off the \\ in log\\.level so that you get the correct key to ignore.

.filter(key => {
return typeof log[key] === 'string' ||
typeof log[key] === 'number' ||
typeof log[key] === 'boolean'
})
const prettifiedObject = prettifyObject({
log,
skipKeys,
Expand Down
16 changes: 16 additions & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,22 @@ test('basic prettifier tests', (t) => {
log.info({ msg: 'foo', bar: 'warn' })
})

t.test('can use nested level keys', (t) => {
t.plan(1)
const pretty = prettyFactory({ levelKey: 'log\\.level' })
const log = pino({}, new Writable({
write (chunk, enc, cb) {
const formatted = pretty(chunk.toString())
t.equal(
formatted,
`[${formattedEpoch}] WARN (${pid}): foo\n`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this fix, you would get log.level underneath in the prettified object.

)
cb()
}
}))
log.info({ msg: 'foo', 'log.level': 'warn' })
})

t.test('can use a customPrettifier on default level output', (t) => {
t.plan(1)
const veryCustomLevels = {
Expand Down