Skip to content

Commit d00c01e

Browse files
committed
Update LoggingHandler to stringify record.msg
Update LoggingHandler so that it converts the log record message to a string. The message may be any arbitrary object, and currently non-string messages are being sent as objects, which the APM Server rejects.
1 parent 57f4010 commit d00c01e

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

elasticapm/handlers/logging.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def _emit(self, record, **kwargs):
130130

131131
return self.client.capture(
132132
"Message",
133-
param_message={"message": record.msg, "params": record.args},
133+
param_message={"message": compat.text_type(record.msg), "params": record.args},
134134
stack=stack,
135135
custom=custom,
136136
exception=exception,

tests/handlers/logging/logging_tests.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,11 @@ def test_logging_handler_dont_emit_elasticapm(capsys, elasticapm_client):
203203
handler.emit(LogRecord("elasticapm.errors", 1, "/ab/c/", 10, "Oops", [], None))
204204
out, err = capsys.readouterr()
205205
assert "Oops" in err
206+
207+
208+
def test_arbitrary_object(logger):
209+
logger.error(["a", "list", "of", "strings"])
210+
assert len(logger.client.events) == 1
211+
event = logger.client.events.pop(0)["errors"][0]
212+
assert "param_message" in event["log"]
213+
assert event["log"]["param_message"] == "['a', 'list', 'of', 'strings']"

0 commit comments

Comments
 (0)