Skip to content

Commit

Permalink
fix: adds support for printing number and boolean messageKey value ty…
Browse files Browse the repository at this point in the history
…pe (#434)

* fix: adds support for printing number and boolean messageKey value type

closes: Pino Pretty does not output numbers but only strings. #433

* Update test/basic.test.js

Co-authored-by: James Sumners <james@sumners.email>

* Update test/basic.test.js

Co-authored-by: James Sumners <james@sumners.email>

* Update test/basic.test.js

Co-authored-by: James Sumners <james@sumners.email>

* fix: checks for undefined specifically instead of falsey values

---------

Co-authored-by: James Sumners <james@sumners.email>
  • Loading branch information
rjmohammad and jsumners committed Jul 4, 2023
1 parent 5d64b47 commit c32de73
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
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)
})

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

0 comments on commit c32de73

Please sign in to comment.