Skip to content

% symbol in object interpreted as formatter #766

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

Open
matthieusieben opened this issue Jun 23, 2020 · 6 comments
Open

% symbol in object interpreted as formatter #766

matthieusieben opened this issue Jun 23, 2020 · 6 comments
Labels
bug This issue identifies a malfunction change-patch This proposes or provides a change that requires a patch release help-wanted This issue has an actionable item pr-welcome This issue has an approved change; a pull request would be appreciated

Comments

@matthieusieben
Copy link

matthieusieben commented Jun 23, 2020

const debug = require('debug')('foo')
debug.enabled = true // Or use ENV variables
const someObjectIWantToLog = { "%j": "%j %j %%" }
debug("%o", someObjectIWantToLog, 1, 2, 3)

result:

  foo { '1': '2 3 %' } +0ms

expected result:

  foo { "%j": "%j %j %%" } 1 2 3 +0ms

This gets even weirder when running the following several times:

debug("%o", { "%j": "%j %j %%" })
debug("%o", { "%j": "%j %j %%" })
debug("%o", { "%j": "%j %j %%" })
debug("%o", { "%j": "%j %j %%" })

result:

  foo { '"\u001b[38;5;148m+3s\u001b[0m"': '%j %j %' }
  foo { '"\u001b[38;5;148m+847ms\u001b[0m"': '%j %j %' }
  foo { '"\u001b[38;5;148m+674ms\u001b[0m"': '%j %j %' }
  foo { '"\u001b[38;5;148m+620ms\u001b[0m"': '%j %j %' }

Did I miss something in the doc? Is there a way not to interpret those % symbols? Or is this a bug?

@Qix-
Copy link
Member

Qix- commented Jun 23, 2020

Reproduced; this is very clearly a bug.

Sorry you're hitting it - I'm surprised it's not been brought up before. Thank you for the reproduction steps.

PR is welcome, else I will try to find time within the next week to get to it.

@Qix- Qix- added bug This issue identifies a malfunction change-patch This proposes or provides a change that requires a patch release help-wanted This issue has an actionable item pr-welcome This issue has an approved change; a pull request would be appreciated labels Jun 23, 2020
@Qix-
Copy link
Member

Qix- commented Jun 23, 2020

Super weird. Just investigated a bit more to make sure this wasn't introduced recently (it wasn't, it exists pre-4.x).

It appears, for whatever reason, the %o is honoring the second argument as another formatting string.

If you would like to work around this, insert another %o at the beginning of the argument list. I know this isn't ideal, but if you need a fix right now, that should get you somewhat sane output.

const debug = require('debug')('foo');
debug.enabled = true // Or use ENV variables

const someObjectIWantToLog = { "%j": "%j %j %%" }
debug("%o", "%o", someObjectIWantToLog, 1, 2, 3)
debug("%o", "%o", { "%j": "%j %j %%" })
debug("%o", "%o", { "%j": "%j %j %%" })
debug("%o", "%o", { "%j": "%j %j %%" })
debug("%o", "%o", { "%j": "%j %j %%" })
  foo '{ '%j': '%j %j %%' }' 1 2 3 +0ms
  foo '{ '%j': '%j %j %%' }' +2ms
  foo '{ '%j': '%j %j %%' }' +1ms
  foo '{ '%j': '%j %j %%' }' +0ms
  foo '{ '%j': '%j %j %%' }' +0ms

@Sarahho0624

This comment has been minimized.

@webketje
Copy link

webketje commented Jun 3, 2022

@qix FWIW EJS templates (which use <% %> as delimiters by default) have worked around this by introducing the convention that pre- or appending the delimiter will interpret it as literal. Could be a viable, simple option that eg %%o will output a literal %o

@RabiaSaid
Copy link

2.6.8

@RabiaSaid
Copy link

4.3.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue identifies a malfunction change-patch This proposes or provides a change that requires a patch release help-wanted This issue has an actionable item pr-welcome This issue has an approved change; a pull request would be appreciated
Development

No branches or pull requests

5 participants