Skip to content

Commit 9ab85a3

Browse files
EvaBardouEvaBardou
andauthored
Fix "Attempt to overwrite '*' in LogRecord" (#1276)
Co-authored-by: EvaBardou <ebardou@teklia.com>
1 parent add86d9 commit 9ab85a3

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

tools/code_review_tools/log.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,26 @@ def setup_sentry(name, channel, dsn):
8484
sentry_sdk.set_context("task", {"task_id": task_id})
8585

8686

87+
class RenameAttrsProcessor(structlog.processors.KeyValueRenderer):
88+
"""
89+
Rename event_dict keys that will attempt to overwrite LogRecord common
90+
attributes during structlog.stdlib.render_to_log_kwargs processing
91+
"""
92+
93+
def __call__(self, logger, method_name, event_dict):
94+
to_rename = [
95+
key
96+
for key in event_dict
97+
if key in sentry_sdk.integrations.logging.COMMON_RECORD_ATTRS
98+
]
99+
100+
for key in to_rename:
101+
event_dict[f"{key}_"] = event_dict[key]
102+
event_dict.pop(key)
103+
104+
return event_dict
105+
106+
87107
def init_logger(
88108
project_name,
89109
channel=None,
@@ -116,6 +136,7 @@ def init_logger(
116136
structlog.stdlib.PositionalArgumentsFormatter(),
117137
structlog.processors.StackInfoRenderer(),
118138
structlog.processors.format_exc_info,
139+
RenameAttrsProcessor(),
119140
# Transpose the 'event_dict' from structlog into keyword arguments for logging.log
120141
# E.g.: 'event' become 'msg' and, at the end, all remaining values from 'event_dict'
121142
# are added as 'extra'

0 commit comments

Comments
 (0)