pino-pretty
Advanced tools
Comparing version
@@ -45,3 +45,4 @@ 'use strict' | ||
createDate, | ||
isValidDate | ||
isValidDate, | ||
interpretConditionals | ||
} | ||
@@ -267,12 +268,17 @@ | ||
if (messageFormat && typeof messageFormat === 'string') { | ||
const message = String(messageFormat).replace(/{([^{}]+)}/g, function (match, p1) { | ||
// return log level as string instead of int | ||
let level | ||
if (p1 === levelLabel && (level = getPropertyValue(log, levelKey)) !== undefined) { | ||
const condition = useOnlyCustomProps ? customLevels === undefined : customLevels[level] === undefined | ||
return condition ? LEVELS[level] : customLevels[level] | ||
} | ||
// Parse nested key access, e.g. `{keyA.subKeyB}`. | ||
return getPropertyValue(log, p1) || '' | ||
}) | ||
const parsedMessageFormat = interpretConditionals(messageFormat, log) | ||
const message = String(parsedMessageFormat).replace( | ||
/{([^{}]+)}/g, | ||
function (match, p1) { | ||
// return log level as string instead of int | ||
let level | ||
if (p1 === levelLabel && (level = getPropertyValue(log, levelKey)) !== undefined) { | ||
const condition = useOnlyCustomProps ? customLevels === undefined : customLevels[level] === undefined | ||
return condition ? LEVELS[level] : customLevels[level] | ||
} | ||
// Parse nested key access, e.g. `{keyA.subKeyB}`. | ||
return getPropertyValue(log, p1) || '' | ||
}) | ||
return colorizer.message(message) | ||
@@ -388,3 +394,3 @@ } | ||
if (keysToIgnore.includes(k) === false) { | ||
// Pre-apply custom prettifiers, because all 3 cases below will need this | ||
// Pre-apply custom prettifiers, because all 3 cases below will need this | ||
const pretty = typeof customPrettifiers[k] === 'function' | ||
@@ -563,3 +569,3 @@ ? customPrettifiers[k](v, k, input) | ||
* @param {string|string[]} property A string, or an array of strings, identifying | ||
* the property to be retrieved from the object. | ||
* the property to be retrieved from the object. | ||
* Accepts nested properties delimited by a `.`. | ||
@@ -773,1 +779,32 @@ * Delimiter can be escaped to preserve property names that contain the delimiter. | ||
} | ||
/** | ||
* Translates all conditional blocks from within the messageFormat. Translates any matching | ||
* {if key}{key}{end} statements and returns everything between if and else blocks if the key provided | ||
* was found in log. | ||
* | ||
* @param {string} messageFormat A format string or function that defines how the | ||
* logged message should be conditionally formatted, e.g. `'{if level}{level}{end} - {if req.id}{req.id}{end}'`. | ||
* @param {object} log The log object to be modified. | ||
* | ||
* @returns {string} The parsed messageFormat. | ||
*/ | ||
function interpretConditionals (messageFormat, log) { | ||
messageFormat = messageFormat.replace(/{if (.*?)}(.*?){end}/g, replacer) | ||
// Remove non-terminated if blocks | ||
messageFormat = messageFormat.replace(/{if (.*?)}/g, '') | ||
// Remove floating end blocks | ||
messageFormat = messageFormat.replace(/{end}/g, '') | ||
return messageFormat.replace(/\s+/g, ' ').trim() | ||
function replacer (_, key, value) { | ||
const propertyValue = getPropertyValue(log, key) | ||
if (propertyValue && value.includes(key)) { | ||
return value.replace(new RegExp('{' + key + '}', 'g'), propertyValue) | ||
} else { | ||
return '' | ||
} | ||
} | ||
} |
{ | ||
"name": "pino-pretty", | ||
"version": "10.1.0", | ||
"version": "10.2.0", | ||
"description": "Prettifier for Pino log lines", | ||
@@ -14,3 +14,3 @@ "type": "commonjs", | ||
"lint": "standard | snazzy", | ||
"test": "tap --100 --color", | ||
"test": "tap", | ||
"test-types": "tsc && tsd" | ||
@@ -17,0 +17,0 @@ }, |
@@ -346,2 +346,10 @@ <a id="intro"></a> | ||
In addition to this, if / end statement blocks can also be specified. Else statements and nested conditions are not supported. | ||
```js | ||
{ | ||
messageFormat: '{levelLabel} - {if pid}{pid} - {end}url:{req.url}' | ||
} | ||
``` | ||
This option can also be defined as a `function` with this prototype: | ||
@@ -348,0 +356,0 @@ |
@@ -225,1 +225,68 @@ 'use strict' | ||
}) | ||
tap.test('#interpretConditionals', t => { | ||
const logData = { | ||
level: 30, | ||
data1: { | ||
data2: 'bar' | ||
}, | ||
msg: 'foo' | ||
} | ||
t.test('interpretConditionals translates if / else statement to found property value', async t => { | ||
const log = fastCopy(logData) | ||
t.equal(internals.interpretConditionals('{level} - {if data1.data2}{data1.data2}{end}', log), '{level} - bar') | ||
}) | ||
t.test('interpretConditionals translates if / else statement to found property value and leave unmatched property key untouched', async t => { | ||
const log = fastCopy(logData) | ||
t.equal(internals.interpretConditionals('{level} - {if data1.data2}{data1.data2} ({msg}){end}', log), '{level} - bar ({msg})') | ||
}) | ||
t.test('interpretConditionals removes non-terminated if statements', async t => { | ||
const log = fastCopy(logData) | ||
t.equal(internals.interpretConditionals('{level} - {if data1.data2}{data1.data2}', log), '{level} - {data1.data2}') | ||
}) | ||
t.test('interpretConditionals removes floating end statements', async t => { | ||
const log = fastCopy(logData) | ||
t.equal(internals.interpretConditionals('{level} - {data1.data2}{end}', log), '{level} - {data1.data2}') | ||
}) | ||
t.test('interpretConditionals removes floating end statements within translated if / end statements', async t => { | ||
const log = fastCopy(logData) | ||
t.equal(internals.interpretConditionals('{level} - {if msg}({msg}){end}{end}', log), '{level} - (foo)') | ||
}) | ||
t.test('interpretConditionals removes if / end blocks if existent condition key does not match existent property key', async t => { | ||
const log = fastCopy(logData) | ||
t.equal(internals.interpretConditionals('{level}{if msg}{data1.data2}{end}', log), '{level}') | ||
}) | ||
t.test('interpretConditionals removes if / end blocks if non-existent condition key does not match existent property key', async t => { | ||
const log = fastCopy(logData) | ||
t.equal(internals.interpretConditionals('{level}{if foo}{msg}{end}', log), '{level}') | ||
}) | ||
t.test('interpretConditionals removes if / end blocks if existent condition key does not match non-existent property key', async t => { | ||
const log = fastCopy(logData) | ||
t.equal(internals.interpretConditionals('{level}{if msg}{foo}{end}', log), '{level}') | ||
}) | ||
t.test('interpretConditionals removes if / end blocks if non-existent condition key does not match non-existent property key', async t => { | ||
const log = fastCopy(logData) | ||
t.equal(internals.interpretConditionals('{level}{if foo}{bar}{end}', log), '{level}') | ||
}) | ||
t.test('interpretConditionals removes if / end blocks if nested condition key does not match property key', async t => { | ||
const log = fastCopy(logData) | ||
t.equal(internals.interpretConditionals('{level}{if data1.msg}{data1.data2}{end}', log), '{level}') | ||
}) | ||
t.test('interpretConditionals removes nested if / end statement blocks', async t => { | ||
const log = fastCopy(logData) | ||
t.equal(internals.interpretConditionals('{if msg}{if data1.data2}{msg}{data1.data2}{end}{end}', log), 'foo{data1.data2}') | ||
}) | ||
t.end() | ||
}) |
@@ -144,2 +144,7 @@ 'use strict' | ||
t.test('`messageFormat` supports conditional blocks', async t => { | ||
const str = prettifyMessage({ log: { level: 30, req: { id: 'foo' } }, messageFormat: '{level} | {if req.id}({req.id}){end}{if msg}{msg}{end}' }) | ||
t.equal(str, '30 | (foo)') | ||
}) | ||
t.test('`messageFormat` supports function definition', async t => { | ||
@@ -146,0 +151,0 @@ const str = prettifyMessage({ |
Sorry, the diff of this file is not supported yet
192390
1.97%4265
2.13%380
2.15%