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

feat: allow prettifier for messageKey as number #302

Closed
wants to merge 3 commits into from
Closed
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
6 changes: 3 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ function prettifyLevel ({ log, colorizer = defaultColorizer, levelKey = LEVEL_KE
*
* @param {object} input
* @param {object} input.log The log object with the message to colorize.
* @param {string} [input.messageKey='msg'] The property of the `log` that is the
* @param {string|number} [input.messageKey='msg'] The property of the `log` that is the
* message to be prettified.
* @param {string|function} [input.messageFormat=undefined] A format string or function that defines how the
* logged message should be formatted, e.g. `'{level} - {pid}'`.
Expand All @@ -249,7 +249,7 @@ function prettifyLevel ({ log, colorizer = defaultColorizer, levelKey = LEVEL_KE
* @param {object} [input.customLevels] The custom levels where key as the level index and value as the level name.
*
* @returns {undefined|string} If the message key is not found, or the message
* key is not a string, then `undefined` will be returned. Otherwise, a string
* key is not a string, nor a number, then `undefined` will be returned. Otherwise, a string
* that is the prettified message.
*/
function prettifyMessage ({ log, messageFormat, messageKey = MESSAGE_KEY, colorizer = defaultColorizer, levelLabel = LEVEL_LABEL, levelKey = LEVEL_KEY, customLevels }) {
Expand All @@ -274,7 +274,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') return undefined
return colorizer.message(log[messageKey])
}

Expand Down
2 changes: 1 addition & 1 deletion test/lib/utils.public.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ tap.test('prettifyMessage', t => {
t.equal(str, undefined)
})

t.test('returns `undefined` if `messageKey` not string', async t => {
t.test('returns `undefined` if `messageKey` not string nor number', async t => {
const str = prettifyMessage({ log: { msg: {} } })
t.equal(str, undefined)
})
Expand Down