Skip to content

Commit af5cd2d

Browse files
committed
Leverage caplog handler as suggested by @grahamalama
1 parent 73a4d78 commit af5cd2d

File tree

1 file changed

+3
-9
lines changed

1 file changed

+3
-9
lines changed

tests/fastapi/test_fastapi.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# This Source Code Form is subject to the terms of the Mozilla Public
22
# License, v. 2.0. If a copy of the MPL was not distributed with this
33
# file, you can obtain one at http://mozilla.org/MPL/2.0/.
4-
import io
54
import json
65
import logging
76

@@ -95,13 +94,8 @@ def test_mozlog_without_correlation_id_middleware(client, caplog):
9594

9695

9796
def test_request_id_passed_to_all_log_messages(caplog):
98-
buffer = io.StringIO()
99-
handler = logging.StreamHandler(stream=buffer)
100-
handler.addFilter(RequestIdLogFilter())
101-
handler.setFormatter(JsonLogFormatter())
102-
103-
_logger = logging.getLogger("some_logger")
104-
_logger.addHandler(handler)
97+
caplog.handler.addFilter(RequestIdLogFilter())
98+
caplog.handler.setFormatter(JsonLogFormatter())
10599

106100
app = create_app()
107101

@@ -115,9 +109,9 @@ def ping():
115109

116110
client.get("/ping")
117111

118-
parsed_log = json.loads(buffer.getvalue())
119112
log_message = next(r for r in caplog.records if r.name == "some_logger")
120113
assert log_message.rid is not None
114+
parsed_log = json.loads(caplog.text.splitlines()[0])
121115
assert "rid" in parsed_log["Fields"]
122116

123117

0 commit comments

Comments
 (0)