Repository navigation
Handle message formatting errors - #329
Conversation
Deploying logfire-docs with
|
| 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 |
Codecov ReportAll modified and coverable lines are covered by tests ✅ 📢 Thoughts on this report? Let us know! |
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' |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
That makes sense, given that I'm good with just leaving the message as is. 👍
dmontagu
left a comment
There was a problem hiding this comment.
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.
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.