From bdd763006222bbad46f1aead1f2196740ca9c6fe Mon Sep 17 00:00:00 2001 From: Graham Beckley Date: Thu, 4 Apr 2024 15:37:50 -0400 Subject: [PATCH] (fastapi) Initialize JsonLogFormatter to pass to handler --- src/dockerflow/fastapi/middleware.py | 2 +- tests/fastapi/test_fastapi.py | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/dockerflow/fastapi/middleware.py b/src/dockerflow/fastapi/middleware.py index 9096de9..02a844e 100644 --- a/src/dockerflow/fastapi/middleware.py +++ b/src/dockerflow/fastapi/middleware.py @@ -59,7 +59,7 @@ def __init__( logger.setLevel(logging.INFO) handler = logging.StreamHandler(sys.stdout) handler.setLevel(logging.INFO) - handler.setFormatter(JsonLogFormatter) + handler.setFormatter(JsonLogFormatter()) logger.addHandler(handler) self.logger = logger diff --git a/tests/fastapi/test_fastapi.py b/tests/fastapi/test_fastapi.py index 3a496e1..4ec97de 100644 --- a/tests/fastapi/test_fastapi.py +++ b/tests/fastapi/test_fastapi.py @@ -47,7 +47,21 @@ def test_lbheartbeat_head(client): assert response.content == b"" -def test_mozlog(app, client, caplog): +def test_mozlog_record_formatted_as_json(app, client, capsys): + app.state.DOCKERFLOW_SUMMARY_LOG_QUERYSTRING = True + + client.get( + "/__lbheartbeat__?x=شكر", + headers={ + "User-Agent": "dockerflow/tests", + "Accept-Language": "en-US", + }, + ) + stdout = capsys.readouterr().out + assert json.loads(stdout) + + +def test_mozlog_record_attrs(app, client, caplog): app.state.DOCKERFLOW_SUMMARY_LOG_QUERYSTRING = True client.get(