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

Add support for messageFormat strings containing conditionals #442

Merged
merged 7 commits into from
Jul 25, 2023

Conversation

timlohse1104
Copy link
Contributor

@timlohse1104 timlohse1104 commented Jul 23, 2023

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.

@coveralls
Copy link

coveralls commented Jul 23, 2023

Pull Request Test Coverage Report for Build 5649830685

  • 14 of 14 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 100.0%

Totals Coverage Status
Change from base Build 5620622122: 0.0%
Covered Lines: 419
Relevant Lines: 419

💛 - Coveralls

Copy link
Member

@mcollina mcollina left a 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}')
})
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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
@timlohse1104
Copy link
Contributor Author

Updated documentation with --messageFormat condition example.
Add matching for condition and property keys.
Add further unit tests for parseMessageFormat function.

Readme.md Outdated Show resolved Hide resolved
lib/utils.js Outdated
Comment on lines 787 to 803
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()
}
Copy link
Member

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:

Suggested change
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 ''
}
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated accordingly.

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 => {
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed wording.

t.equal(internals.parseMessageFormat('{level} - {if data1.data2}{data1.data2}', log), '{level} - {data1.data2}')
})

t.test('parseMessageFormat removes unescaped end statements', async t => {
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed wording.

t.equal(internals.parseMessageFormat('{level} - {if data1.data2}{data1.data2}', log), '{level} - {data1.data2}')
})

t.test('parseMessageFormat removes unescaped end statements', async t => {
Copy link
Member

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)

Copy link
Contributor Author

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) {
Copy link
Member

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:

Suggested change
function parseMessageFormat (messageFormat, log) {
function interpretConditionals (messageFormat, log) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed accordingly.

@timlohse1104
Copy link
Contributor Author

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.

@jsumners
Copy link
Member

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
lib/utils.js Outdated Show resolved Hide resolved
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Copy link
Member

@jsumners jsumners left a 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.

@jsumners jsumners merged commit ff7b238 into pinojs:master Jul 25, 2023
6 checks passed
renovate bot referenced this pull request in fwouts/previewjs Jul 25, 2023
[![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 [@&#8203;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
[@&#8203;timlohse1104](https://togithub.com/timlohse1104) in
[https://github.com/pinojs/pino-pretty/pull/442](https://togithub.com/pinojs/pino-pretty/pull/442)

#### New Contributors

- [@&#8203;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>
bodinsamuel referenced this pull request in specfy/specfy Aug 7, 2023
[![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 [@&#8203;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
[@&#8203;timlohse1104](https://togithub.com/timlohse1104) in
[https://github.com/pinojs/pino-pretty/pull/442](https://togithub.com/pinojs/pino-pretty/pull/442)

#### New Contributors

- [@&#8203;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
[@&#8203;Hufschmidt](https://togithub.com/Hufschmidt) in
[https://github.com/pinojs/pino-pretty/pull/440](https://togithub.com/pinojs/pino-pretty/pull/440)

#### New Contributors

- [@&#8203;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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants