Skip to content

Commit 7c28d14

Browse files
committed
fix(ourlogs): Use repr instead of json for message and arguments
1 parent 2dde2fe commit 7c28d14

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

sentry_sdk/integrations/logging.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import json
21
import logging
32
from datetime import datetime, timezone
43
from fnmatch import fnmatch
@@ -360,14 +359,14 @@ def _capture_log_from_record(client, record):
360359
otel_severity_number, otel_severity_text = _python_level_to_otel(record.levelno)
361360
attrs = {
362361
"sentry.message.template": (
363-
record.msg if isinstance(record.msg, str) else json.dumps(record.msg)
362+
record.msg if isinstance(record.msg, str) else repr(record.msg)
364363
),
365364
} # type: dict[str, str | bool | float | int]
366365
if record.args is not None:
367366
if isinstance(record.args, tuple):
368367
for i, arg in enumerate(record.args):
369368
attrs[f"sentry.message.parameters.{i}"] = (
370-
arg if isinstance(arg, str) else json.dumps(arg)
369+
arg if isinstance(arg, str) else repr(arg)
371370
)
372371
if record.lineno:
373372
attrs["code.line.number"] = record.lineno

tests/test_logs.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,17 @@ def test_no_log_infinite_loop(sentry_init, capture_envelopes):
281281
python_logger.debug("this is %s a template %s", "1", "2")
282282

283283
assert len(envelopes) == 1
284+
285+
286+
@minimum_python_37
287+
def test_logging_errors(sentry_init, capture_envelopes):
288+
"""
289+
The python logger module should be able to log errors without erroring
290+
"""
291+
sentry_init(_experiments={"enable_sentry_logs": True})
292+
envelopes = capture_envelopes()
293+
294+
python_logger = logging.Logger("test-logger")
295+
python_logger.error(Exception("test exc"))
296+
297+
assert len(envelopes) == 2

0 commit comments

Comments
 (0)