-
Notifications
You must be signed in to change notification settings - Fork 150
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
Add support for messageFormat strings containing conditionals #442
Conversation
Pull Request Test Coverage Report for Build 5649830685
💛 - Coveralls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good work! Could you add some docs?
t.test('parseMessageFormat removes unescaped end statements', async t => { | ||
const log = fastCopy(logData) | ||
t.equal(internals.parseMessageFormat('{level} - {data1.data2}{end}', log), '{level} - {data1.data2}') | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a few tests for not-matching if/else/end pair? What error would you get?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah for sure. Thanks for the fast code review.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added further test scenarios.
…ition and property key and add further unit tests for parseMessageFormat function
Updated documentation with |
Co-authored-by: Matteo Collina <matteo.collina@gmail.com>
lib/utils.js
Outdated
function parseMessageFormat (messageFormat, log) { | ||
messageFormat = messageFormat.replace(/{if (.*?)}(.*?){end}/g, function (_, key, value) { | ||
const propertyValue = getPropertyValue(log, key) | ||
if (propertyValue && value.includes(key)) { | ||
return value.replace(new RegExp('{' + key + '}', 'g'), propertyValue) | ||
} else { | ||
return '' | ||
} | ||
}) | ||
|
||
// Remove unescaped if blocks | ||
messageFormat = messageFormat.replace(/{if (.*?)}/g, '') | ||
// Remove unescaped end blocks | ||
messageFormat = messageFormat.replace(/{end}/g, '') | ||
|
||
return messageFormat.replace(/\s+/g, ' ').trim() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this would be a bit easier to read:
function parseMessageFormat (messageFormat, log) { | |
messageFormat = messageFormat.replace(/{if (.*?)}(.*?){end}/g, function (_, key, value) { | |
const propertyValue = getPropertyValue(log, key) | |
if (propertyValue && value.includes(key)) { | |
return value.replace(new RegExp('{' + key + '}', 'g'), propertyValue) | |
} else { | |
return '' | |
} | |
}) | |
// Remove unescaped if blocks | |
messageFormat = messageFormat.replace(/{if (.*?)}/g, '') | |
// Remove unescaped end blocks | |
messageFormat = messageFormat.replace(/{end}/g, '') | |
return messageFormat.replace(/\s+/g, ' ').trim() | |
} | |
function parseMessageFormat (messageFormat, log) { | |
messageFormat = messageFormat.replace(/{if (.*?)}(.*?){end}/g, replacer) | |
// Remove unescaped if blocks | |
messageFormat = messageFormat.replace(/{if (.*?)}/g, '') | |
// Remove unescaped 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 '' | |
} | |
} | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated accordingly.
test/lib/utils.internals.test.js
Outdated
t.equal(internals.parseMessageFormat('{level} - {if data1.data2}{data1.data2} ({msg}){end}', log), '{level} - bar ({msg})') | ||
}) | ||
|
||
t.test('parseMessageFormat removes unescaped if statements', async t => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe you mean non-terminated instead of unescaped?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed wording.
test/lib/utils.internals.test.js
Outdated
t.equal(internals.parseMessageFormat('{level} - {if data1.data2}{data1.data2}', log), '{level} - {data1.data2}') | ||
}) | ||
|
||
t.test('parseMessageFormat removes unescaped end statements', async t => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think "floating" is a better term than "unescaped" here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed wording.
test/lib/utils.internals.test.js
Outdated
t.equal(internals.parseMessageFormat('{level} - {if data1.data2}{data1.data2}', log), '{level} - {data1.data2}') | ||
}) | ||
|
||
t.test('parseMessageFormat removes unescaped end statements', async t => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if the format string is?:
{level} - {if foo}something{end}{end}
(i.e. someone tried to add an unsupported nested condition but failed to add the leading if
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The additional {end}
within the if / end statement will be removed. Added test scenario for this.
lib/utils.js
Outdated
* | ||
* @returns {string} The parsed messageFormat. | ||
*/ | ||
function parseMessageFormat (messageFormat, log) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this would be a better name:
function parseMessageFormat (messageFormat, log) { | |
function interpretConditionals (messageFormat, log) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed accordingly.
Hey @jsumners thanks for the code review. I will look into it after work. Please excuse my poor english var and function wording. Will edit. |
No problem. The review phase is there to get us into an agreeable state. |
…teral unit test scenario, refactored interpretConditionals logic to be more readable and renamed internal unit tests to be more fitting
Co-authored-by: James Sumners <321201+jsumners@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com) This PR contains the following updates: | Package | Change | Age | Adoption | Passing | Confidence | |---|---|---|---|---|---| | [pino-pretty](https://togithub.com/pinojs/pino-pretty) | [`^10.1.0` -> `^10.2.0`](https://renovatebot.com/diffs/npm/pino-pretty/10.1.0/10.2.0) | [![age](https://developer.mend.io/api/mc/badges/age/npm/pino-pretty/10.2.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/pino-pretty/10.2.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/pino-pretty/10.1.0/10.2.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/pino-pretty/10.1.0/10.2.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes <details> <summary>pinojs/pino-pretty (pino-pretty)</summary> ### [`v10.2.0`](https://togithub.com/pinojs/pino-pretty/releases/tag/v10.2.0) [Compare Source](https://togithub.com/pinojs/pino-pretty/compare/v10.1.0...v10.2.0) #### What's Changed - Remove coveralls by [@​jsumners](https://togithub.com/jsumners) in [https://github.com/pinojs/pino-pretty/pull/443](https://togithub.com/pinojs/pino-pretty/pull/443) - Add support for messageFormat strings containing conditionals by [@​timlohse1104](https://togithub.com/timlohse1104) in [https://github.com/pinojs/pino-pretty/pull/442](https://togithub.com/pinojs/pino-pretty/pull/442) #### New Contributors - [@​timlohse1104](https://togithub.com/timlohse1104) made their first contribution in [https://github.com/pinojs/pino-pretty/pull/442](https://togithub.com/pinojs/pino-pretty/pull/442) **Full Changelog**: pinojs/pino-pretty@v10.1.0...v10.2.0 </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Enabled. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about these updates again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://developer.mend.io/github/fwouts/previewjs). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNi4xMS4wIiwidXBkYXRlZEluVmVyIjoiMzYuMTEuMCIsInRhcmdldEJyYW5jaCI6Im1haW4ifQ==--> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com) This PR contains the following updates: | Package | Change | Age | Adoption | Passing | Confidence | |---|---|---|---|---|---| | [pino-pretty](https://togithub.com/pinojs/pino-pretty) | [`10.0.0` -> `10.2.0`](https://renovatebot.com/diffs/npm/pino-pretty/10.0.0/10.2.0) | [![age](https://developer.mend.io/api/mc/badges/age/npm/pino-pretty/10.2.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/pino-pretty/10.2.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/pino-pretty/10.0.0/10.2.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/pino-pretty/10.0.0/10.2.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes <details> <summary>pinojs/pino-pretty (pino-pretty)</summary> ### [`v10.2.0`](https://togithub.com/pinojs/pino-pretty/releases/tag/v10.2.0) [Compare Source](https://togithub.com/pinojs/pino-pretty/compare/v10.1.0...v10.2.0) #### What's Changed - Remove coveralls by [@​jsumners](https://togithub.com/jsumners) in [https://github.com/pinojs/pino-pretty/pull/443](https://togithub.com/pinojs/pino-pretty/pull/443) - Add support for messageFormat strings containing conditionals by [@​timlohse1104](https://togithub.com/timlohse1104) in [https://github.com/pinojs/pino-pretty/pull/442](https://togithub.com/pinojs/pino-pretty/pull/442) #### New Contributors - [@​timlohse1104](https://togithub.com/timlohse1104) made their first contribution in [https://github.com/pinojs/pino-pretty/pull/442](https://togithub.com/pinojs/pino-pretty/pull/442) **Full Changelog**: pinojs/pino-pretty@v10.1.0...v10.2.0 ### [`v10.1.0`](https://togithub.com/pinojs/pino-pretty/releases/tag/v10.1.0) [Compare Source](https://togithub.com/pinojs/pino-pretty/compare/v10.0.1...v10.1.0) #### What's Changed - Add customColors to typescript definition for PrettyOptions by [@​Hufschmidt](https://togithub.com/Hufschmidt) in [https://github.com/pinojs/pino-pretty/pull/440](https://togithub.com/pinojs/pino-pretty/pull/440) #### New Contributors - [@​Hufschmidt](https://togithub.com/Hufschmidt) made their first contribution in [https://github.com/pinojs/pino-pretty/pull/440](https://togithub.com/pinojs/pino-pretty/pull/440) **Full Changelog**: pinojs/pino-pretty@v10.0.1...v10.1.0 ### [`v10.0.1`](https://togithub.com/pinojs/pino-pretty/releases/tag/v10.0.1) [Compare Source](https://togithub.com/pinojs/pino-pretty/compare/v10.0.0...v10.0.1) **Full Changelog**: pinojs/pino-pretty@v9.4.1...v10.0.1 </details> --- ### Configuration 📅 **Schedule**: Branch creation - "after 4pm on friday,before 9am on monday,every weekend" in timezone Europe/Paris, Automerge - At any time (no schedule defined). 🚦 **Automerge**: Enabled. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://developer.mend.io/github/specfy/specfy). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNi4yNy4xIiwidXBkYXRlZEluVmVyIjoiMzYuMjcuMSIsInRhcmdldEJyYW5jaCI6ImNob3JlL3Jlbm92YXRlQmFzZUJyYW5jaCJ9--> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
See #441
It's now possible to pass
--messageFormat
option containing conditional statements for log data.Example log data:
{level: 30, req: {id: 'foo'}}
Example
--messageFormat
containing conditional:"{level} - {if req.id}({req.id}){end}{if msg} - {msg}{end}"
Example output:
"30 - (foo)"
Unescaped if / end block within
--messageFormat
will just be deleted.