Skip to content

Handle message formatting errors - #329

Merged
alexmojaki merged 22 commits into
mainfrom
alex/formatting-errors
Jul 24, 2024
Merged

alexmojaki merged 22 commits into
mainfrom
alex/formatting-errors

Conversation

@alexmojaki

@alexmojaki alexmojaki commented Jul 23, 2024 •

Copy link
Copy Markdown
Contributor

Addresses https://pydanticlogfire.slack.com/archives/C06EDRBSAH3/p1721433723530879

There were several places where we explicitly or implicitly raised errors which could lead to similar complaints from users. Many of these errors could be caused in the same way: by passing a preformatted string which gets unintentionally interpreted as a formatting template. I've converted all these known cases to warnings.

Previously we would warn about missing fields but continue trying to format the string. This had the small benefit of formatting other fields that weren't present, so the user gets something partially formatted. This helped in the specific case where they know what they're doing but made a typo. Keeping this behaviour would mean that something like logfire.info("log {'1': {'2': {'3': {'4': 5}}}}")would produce three warnings about missing fields '1', '2', and '3', and a fourth warning about recursion depth, all from the same line. So instead I chose to just fail quickly when a field is missing. This just means that the message is just the format string, but the user can still find any correct fields in the attributes, so it's not a big loss. Also the better way to prevent these kinds of typos is to use an f-string, and the warning points to that page in the docs.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 23, 2024 •

Copy link
Copy Markdown

Deploying logfire-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 92f9275
Status: ✅  Deploy successful!
Preview URL: https://9c3460c9.logfire-docs.pages.dev
Branch Preview URL: https://alex-formatting-errors.logfire-docs.pages.dev

View logs

@codecov

codecov Bot commented Jul 23, 2024 •

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

📢 Thoughts on this report? Let us know!

@alexmojaki alexmojaki changed the title Handle formatting errors Handle message formatting errors Jul 24, 2024
@alexmojaki
alexmojaki marked this pull request as ready for review July 24, 2024 12:54
@alexmojaki
alexmojaki requested a review from Kludex July 24, 2024 12:54
@alexmojaki
alexmojaki requested a review from dmontagu July 24, 2024 12:58
Comment thread logfire/_internal/formatter.py Resolved
Co-authored-by: David Montague <35119617+dmontagu@users.noreply.github.com>
f'\n'
f' Ensure you are either:\n'
' (1) passing an f-string directly, with inspect_arguments enabled and working, or\n'
' (2) passing a literal `str.format`-style template, not a preformatted string.\n'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You are also allowed to pass non-string-literals right? (By that I mean, a string-valued variable.) The way this message is phrased it sounds like that isn't allowed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You're allowed, if you know what you're doing. On the one hand we used to have a LiteralString type hint for the argument, and it only got removed to allow passing f-strings. On the other hand the linked docs do suggest a reasonable way to pass a variable string. We could say 'low cardinality' but that's a bit heavy.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That makes sense, given that I'm good with just leaving the message as is. 👍

@dmontagu dmontagu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Other than my note about the string literal message, LGTM. Let me know if you want input on how to tweak that message. Alternatively, feel free to merge without changes if you think it's good.

@alexmojaki
alexmojaki merged commit 46322c9 into main Jul 24, 2024
@alexmojaki
alexmojaki deleted the alex/formatting-errors branch July 24, 2024 14:41
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.

2 participants