Skip to content

Commit

Permalink
Leverage caplog handler as suggested by @grahamalama
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem committed Mar 4, 2024
1 parent 73a4d78 commit af5cd2d
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions tests/fastapi/test_fastapi.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, you can obtain one at http://mozilla.org/MPL/2.0/.
import io
import json
import logging

Expand Down Expand Up @@ -95,13 +94,8 @@ def test_mozlog_without_correlation_id_middleware(client, caplog):


def test_request_id_passed_to_all_log_messages(caplog):
buffer = io.StringIO()
handler = logging.StreamHandler(stream=buffer)
handler.addFilter(RequestIdLogFilter())
handler.setFormatter(JsonLogFormatter())

_logger = logging.getLogger("some_logger")
_logger.addHandler(handler)
caplog.handler.addFilter(RequestIdLogFilter())
caplog.handler.setFormatter(JsonLogFormatter())

app = create_app()

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

client.get("/ping")

parsed_log = json.loads(buffer.getvalue())
log_message = next(r for r in caplog.records if r.name == "some_logger")
assert log_message.rid is not None
parsed_log = json.loads(caplog.text.splitlines()[0])
assert "rid" in parsed_log["Fields"]


Expand Down

0 comments on commit af5cd2d

Please sign in to comment.