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: adds support for printing number and boolean messageKey value type #434

Merged
merged 5 commits into from
Jul 4, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ function prettyFactory (options) {
line += ':'
}

if (prettifiedMessage) {
if (prettifiedMessage !== undefined) {
if (line.length > 0) {
line = `${line} ${prettifiedMessage}`
} else {
Expand All @@ -186,7 +186,7 @@ function prettyFactory (options) {
if (singleLine) line += EOL
line += prettifiedErrorLog
} else if (!hideObject) {
const skipKeys = [messageKey, levelKey, timestampKey].filter(key => typeof log[key] === 'string' || typeof log[key] === 'number')
const skipKeys = [messageKey, levelKey, timestampKey].filter(key => typeof log[key] === 'string' || typeof log[key] === 'number' || typeof log[key] === 'boolean')
const prettifiedObject = prettifyObject({
input: log,
skipKeys,
Expand Down
2 changes: 1 addition & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ function prettifyMessage ({ log, messageFormat, messageKey = MESSAGE_KEY, colori
return colorizer.message(msg)
}
if (messageKey in log === false) return undefined
if (typeof log[messageKey] !== 'string') return undefined
if (typeof log[messageKey] !== 'string' && typeof log[messageKey] !== 'number' && typeof log[messageKey] !== 'boolean') return undefined
return colorizer.message(log[messageKey])
}

Expand Down
64 changes: 64 additions & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,70 @@ test('basic prettifier tests', (t) => {
log.info('foo')
})

t.test('can print message key value when its a string', (t) => {
t.plan(1)
const pretty = prettyFactory()
const log = pino({}, new Writable({
write (chunk, enc, cb) {
const formatted = pretty(chunk.toString())
t.equal(
formatted,
`[${formattedEpoch}] INFO (${pid}): baz\n`
)
cb()
}
}))
log.info('baz')
})

t.test('can print message key value when its a number', (t) => {
t.plan(1)
const pretty = prettyFactory()
const log = pino({}, new Writable({
write (chunk, enc, cb) {
const formatted = pretty(chunk.toString())
t.equal(
formatted,
`[${formattedEpoch}] INFO (${pid}): 42\n`
)
cb()
}
}))
log.info(42)
})

t.test('can print message key value when its a Number(0)', (t) => {
t.plan(1)
const pretty = prettyFactory()
const log = pino({}, new Writable({
write (chunk, enc, cb) {
const formatted = pretty(chunk.toString())
t.equal(
formatted,
`[${formattedEpoch}] INFO (${pid}): 0\n`
)
cb()
}
}))
log.info(0)
})

t.test('can print message key value when its a boolean', (t) => {
t.plan(1)
const pretty = prettyFactory()
const log = pino({}, new Writable({
write (chunk, enc, cb) {
const formatted = pretty(chunk.toString())
t.equal(
formatted,
`[${formattedEpoch}] INFO (${pid}): true\n`
)
cb()
}
}))
log.info(true)
Copy link
Member

Choose a reason for hiding this comment

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

I wonder what log.info(0) or log.info(1) would produce here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added a possible fix for 0.

What is the expectation for log.info(null) or log.info(undefined)?

Copy link
Member

Choose a reason for hiding this comment

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

Only null is valid json. So I imagine we'd just print the word "null". But that can be a separate PR.

})

t.test('can use different message keys', (t) => {
t.plan(1)
const pretty = prettyFactory({ messageKey: 'bar' })
Expand Down