Skip to content

Commit b767230

Browse files
axwbeniwohli
authored andcommitted
Update LoggingHandler to stringify record.msg (#312)
* 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. * added stringification to Logbook handler as well
1 parent 5d61408 commit b767230

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

elasticapm/handlers/logbook.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import logbook
1717

1818
from elasticapm.base import Client
19+
from elasticapm.utils import compat
1920
from elasticapm.utils.encoding import to_unicode
2021

2122
LOOKBOOK_LEVELS = {
@@ -80,7 +81,7 @@ def _emit(self, record):
8081
exception = None
8182

8283
return self.client.capture_message(
83-
param_message={"message": record.msg, "params": record.args},
84+
param_message={"message": compat.text_type(record.msg), "params": record.args},
8485
exception=exception,
8586
level=LOOKBOOK_LEVELS[record.level],
8687
logger_name=record.channel,

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/logbook/logbook_tests.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,12 @@ def test_logbook_handler_dont_emit_elasticapm(capsys, elasticapm_client):
143143
handler.emit(LogRecord("elasticapm.errors", 1, "Oops"))
144144
out, err = capsys.readouterr()
145145
assert "Oops" in err
146+
147+
148+
def test_arbitrary_object(elasticapm_client, logbook_logger, logbook_handler):
149+
with logbook_handler.applicationbound():
150+
logbook_logger.info(["a", "list", "of", "strings"])
151+
assert len(logbook_handler.client.events) == 1
152+
event = logbook_handler.client.events.pop(0)["errors"][0]
153+
assert "param_message" in event["log"]
154+
assert event["log"]["param_message"] == "['a', 'list', 'of', 'strings']"

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)