-
-
Notifications
You must be signed in to change notification settings - Fork 231
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
to_repr
and rich
breaks logging
#655
Comments
Do you install |
I checked which modules are installed on the server and neither |
Thank you! 💛
What you mean here, is that you expect single-line JSON lines and Rich formating breaks them into multiple ones? (is this expected @sscherfke?)
I don't think that's a hot fix – I think you're using configuration as it's intended? 😅
Not to be lecturing, but you should use recursive lock files to make this deterministic. 🤓
We try to avoid having anything global except the configuration and you were able to work around the problem using regular config, so I'm gonna say everything is working as intended? Depending on what Stefan says, we can have a look at multi-line log lines or whatever is happening there, but adding a global variable to avoid configuration is not our MO. :) |
Not entirely. Yes, we expect inline JSON. But whenever we logged an exception, another exception was raised while logging it (I'm not sure about the exact trace content since our logs get removed after some time 🙁). This second exception was raised from the I double-checked the dependencies on the server, and rich is indeed not installed. My hunch is that I should have explained that better, sorry.
Yeah, I guess I meant hotfixing the logging within our service by configuring
I agree. I'm not OG dev on that project and I'm currently pushing for using
I understand. Yes, I managed to configure it correctly. However, as I mentioned at the start - I think the problem was caused by structlog attempting to use rich when it wasn't installed. I haven't investigated thoroughly, but there might be a missing dependency check for rich in structlog.
Sure thing, thanks for your time! |
I'll take a look at it. A (cleaned) traceback from your logs would be really helpful though. Maybe you can reactivate the old behavior on a test system until you get the desired exception? |
Sure thing! I'll try to reproduce the bug and share the trace :) |
This looks like #678 – Rich does not handle exceptions raised during traceback rendering. This applies to the ConsoleRenderer as well. Something like this might be a fix: diff --git a/src/structlog/tracebacks.py b/src/structlog/tracebacks.py
index 2331b2f..2607fc3 100644
--- a/src/structlog/tracebacks.py
+++ b/src/structlog/tracebacks.py
@@ -142,29 +142,32 @@ def to_repr(
implementation.
"""
if use_rich and rich is not None:
- # Let rich render the repr if it is available.
- # It produces much better results for containers and dataclasses/attrs.
- obj_repr = rich.pretty.traverse(
- obj, max_length=max_length, max_string=max_string
- ).render()
- else:
- # Generate a (truncated) repr if rich is not available.
- # Handle str/bytes differently to get better results for truncated
- # representations. Also catch all errors, similarly to "safe_str()".
- try:
- if isinstance(obj, (str, bytes)):
- if max_string is not None and len(obj) > max_string:
- truncated = len(obj) - max_string
- obj_repr = f"{obj[:max_string]!r}+{truncated}"
- else:
- obj_repr = repr(obj)
- else:
- obj_repr = repr(obj)
- if max_string is not None and len(obj_repr) > max_string:
- truncated = len(obj_repr) - max_string
- obj_repr = f"{obj_repr[:max_string]!r}+{truncated}"
- except Exception as error: # noqa: BLE001
- obj_repr = f"<repr-error {str(error)!r}>"
+ try:
+ # Let rich render the repr if it is available.
+ # It produces much better results for containers and dataclasses/attrs.
+ return rich.pretty.traverse(
+ obj, max_length=max_length, max_string=max_string
+ ).render()
+ except Exception:
+ pass
+ # Generate a (truncated) repr if rich is not available.
+ # Handle str/bytes differently to get better results for truncated
+ # representations. Also catch all errors, similarly to "safe_str()".
+ try:
+ if isinstance(obj, (str, bytes)):
+ if max_string is not None and len(obj) > max_string:
+ truncated = len(obj) - max_string
+ obj_repr = f"{obj[:max_string]!r}+{truncated}"
+ else:
+ obj_repr = repr(obj)
+ else:
+ obj_repr = repr(obj)
+ if max_string is not None and len(obj_repr) > max_string:
+ truncated = len(obj_repr) - max_string
+ obj_repr = f"{obj_repr[:max_string]!r}+{truncated}"
+ except Exception as error: # noqa: BLE001
+ obj_repr = f"<repr-error {str(error)!r}>"
return obj_repr |
Structlog version: 24.4.0
Hello,
Maybe this is just bad setup on our end, but it was tricky to figure out so I decided to raise it.
We use
structlog
for logging data from our service in json format, and then uploading it intoDataDog
. After updating structlog to version24.4.0
our logging started to break each time we logged an exception (structlog.get_logger().exception(...)
)I was able to trace the root cause to this PR: #627, where the implementation for
to_repr
changed to:and I was able to hotfix the the problem on our end by creating our own
dict_tracebacks
and settinguse_rich
explicitly.Stack Trace
Since we don't install
rich
on the server, I'm not really sure how it was able to import it in the first place (my guess is transitive dependency), or why it failed.But would it make sense to somehow configure the
use_rich
flag globally?The text was updated successfully, but these errors were encountered: