pino-pretty
Advanced tools
Comparing version 4.4.0 to 4.5.0
@@ -193,3 +193,3 @@ 'use strict' | ||
* 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}'`. | ||
@@ -205,3 +205,3 @@ * @param {function} [input.colorizer] A colorizer function that has a | ||
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) { | ||
@@ -222,2 +222,6 @@ // return log level as string instead of int | ||
} | ||
if (messageFormat && typeof messageFormat === 'function') { | ||
const msg = messageFormat(log, messageKey, levelLabel) | ||
return colorizer.message(msg) | ||
} | ||
if (messageKey in log === false) return undefined | ||
@@ -224,0 +228,0 @@ if (typeof log[messageKey] !== 'string') return undefined |
{ | ||
"name": "pino-pretty", | ||
"version": "4.4.0", | ||
"version": "4.5.0", | ||
"description": "Prettifier for Pino log lines", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -169,2 +169,18 @@ <a id="intro"></a> | ||
`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> | ||
@@ -171,0 +187,0 @@ ## License |
@@ -119,2 +119,14 @@ 'use strict' | ||
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() | ||
@@ -121,0 +133,0 @@ }) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
272053
2261
189