`to_repr` and `rich` breaks logging
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 20/100
Research direction
Read the traceback through structlog/tracebacks.py and the reported use of ExceptionDictTransformer(use_rich=False); the issue does not identify a specific test. First check open pull request #837, which is already addressing this issue. Done means the Rich-related logging failure and the question of configuring use_rich globally are resolved.
Written by the indexing model from the issue text.
Description
Structlog version: 24.4.0
⭐ First of all - love this project, huge fan, good job.
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 into DataDog. After updating structlog to version 24.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: https://github.com/hynek/structlog/pull/627, where the implementation for to_repr changed to:
def to_repr(
obj: Any,
max_length: int | None = None,
max_string: int | None = None,
use_rich: bool = True, # source of the problem
) -> str:
and I was able to hotfix the the problem on our end by creating our own dict_tracebacks and setting use_rich explicitly.
custom_dict_tracebacks = ExceptionRenderer(ExceptionDictTransformer(use_rich=False))
Stack Trace
File "/usr/local/lib/python3.12/site-packages/structlog/_base.py", line 165, in _process_event
event_dict = proc(self._logger, method_name, event_dict)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/structlog/processors.py", line 412, in __call__
event_dict["exception"] = self.format_exception(
^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/structlog/tracebacks.py", line 415, in __call__
trace = extract(
^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/structlog/tracebacks.py", line 276, in extract
key: to_repr(
^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/structlog/tracebacks.py", line 147, in to_repr
obj_repr = rich.pretty.traverse(
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/rich/pretty.py", line 853, in traverse
node = _traverse(_object, root=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/rich/pretty.py", line 767, in _traverse
child_node = _traverse(getattr(obj, field.name), depth=depth + 1)
^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'MemoryObjectItemReceiver' object has no attribute 'item'
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?
- Dominant language
- Python
- Stars
- 5k
- Forks
- 302
- Avg merge
- 17m
- Merged PRs (30d)
- 4
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from hynek/structlog
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
Transform examples in docs into included source filesPossibly taken @alexchen-sys claimed this 97 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 25/100
-
Difficulty 5/5 Over a week Newbie friendliness 38/100
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
-
waiting for feedback
Difficulty 3/5 1-2 days Newbie friendliness 48/100
Similar issues
-
bug good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
yunaremaia/proto-drift#22 ·
-
bug good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
yunaremaia/memwatch#96 ·
-
bug good first issue
Difficulty 1/5 Under an hour Newbie friendliness 91/100
yunaremaia/ci-gate-watch#34 ·
-
bug good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
yunaremaia/agent-capability-attestation#64 ·
Maintainers usually reply within 1 day
-
bug good first issue
Difficulty 1/5 Under an hour Newbie friendliness 86/100
yunaremaia/agent-call-graph#43 ·
Maintainers usually reply within 1 day