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

feature: allow the user to set the messageFormat option as a function #146

Merged
merged 4 commits into from
Feb 1, 2021
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
16 changes: 16 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,22 @@ const prettifyQuery = value => {
}
```

`messageFormat` option allows you to customize the message output. The format can be defined by a template `string` like this:
```js
{
messageFormat: '{levelLabel} - {pid} - url:{request.url}'
}
```
But this option can also be defined as a `function` with this prototype:
```js
{
messageFormat: (log, messageKey, levelLabel) => {
// do some log message customization
return customized_message;
}
}
```

<a id="license"><a>
## License

Expand Down
8 changes: 6 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ function prettifyLevel ({ log, colorizer = defaultColorizer, levelKey = LEVEL_KE
* @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
* message to be prettified.
* @param {string} [input.messageFormat=undefined] A format string that defines how the
* @param {string|function} [input.messageFormat=undefined] A format string or function that defines how the
* logged message should be formatted, e.g. `'{level} - {pid}'`.
* @param {function} [input.colorizer] A colorizer function that has a
* `.message(str)` method attached to it. This function should return a colorized
Expand All @@ -201,7 +201,7 @@ function prettifyLevel ({ log, colorizer = defaultColorizer, levelKey = LEVEL_KE
* that is the prettified message.
*/
function prettifyMessage ({ log, messageFormat, messageKey = MESSAGE_KEY, colorizer = defaultColorizer, levelLabel = LEVEL_LABEL }) {
if (messageFormat) {
if (messageFormat && typeof messageFormat === 'string') {
const message = String(messageFormat).replace(/{([^{}]+)}/g, function (match, p1) {
// return log level as string instead of int
if (p1 === levelLabel && log[LEVEL_KEY]) {
Expand All @@ -217,6 +217,10 @@ function prettifyMessage ({ log, messageFormat, messageKey = MESSAGE_KEY, colori
})
return colorizer.message(message)
}
if (messageFormat && typeof messageFormat === 'function') {
const msg = messageFormat(log, messageKey, levelLabel)
return colorizer.message(msg)
}
if (messageKey in log === false) return undefined
if (typeof log[messageKey] !== 'string') return undefined
return colorizer.message(log[messageKey])
Expand Down
12 changes: 12 additions & 0 deletions test/lib/utils.public.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@ tap.test('prettifyMessage', t => {
t.is(str, 'localhost/test - param: - foo')
})

t.test('`messageFormat` supports function definition', async t => {
const str = prettifyMessage({
log: { level: 30, request: { url: 'localhost/test' }, msg: 'incoming request' },
messageFormat: (log, messageKey, levelLabel) => {
let msg = log[messageKey]
if (msg === 'incoming request') msg = `--> ${log.request.url}`
return msg
}
})
t.is(str, '--> localhost/test')
})

t.end()
})

Expand Down