Skip to content

Commit d880320

Browse files
tpoliawabbiemery
andauthored
chore: Log start of requests at same level as end (#1510)
It is useful when reading through logs to see when each request was received to make sense of subsequent events. --------- Co-authored-by: Abigail Emery <[email protected]>
1 parent d3364b3 commit d880320

2 files changed

Lines changed: 24 additions & 21 deletions

File tree

src/blueapi/service/main.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -621,20 +621,18 @@ async def log_request_details(
621621
) -> Response:
622622
"""Middleware to log all request's host, method, path, status and request and
623623
body"""
624+
log = LOGGER.debug if request.url.path == "/healthz" else LOGGER.info
624625
request_body = await request.body()
625626
client = request.client or Address("Unknown", -1)
626627
log_message = f"{client.host}:{client.port} {request.method} {request.url.path}"
627628
extra = {
628629
"request_body": request_body,
629630
}
630-
LOGGER.debug(log_message, extra=extra)
631+
log(log_message, extra=extra)
631632

632633
response = await call_next(request)
633634
log_message += f" {response.status_code}"
634-
if request.url.path == "/healthz":
635-
LOGGER.debug(log_message, extra=extra)
636-
else:
637-
LOGGER.info(log_message, extra=extra)
635+
log(log_message, extra=extra)
638636

639637
return response
640638

tests/unit_tests/service/test_main.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from unittest import mock
2-
from unittest.mock import Mock
2+
from unittest.mock import Mock, call
33

44
import pytest
55
from fastapi import FastAPI, Request
@@ -29,31 +29,36 @@ async def root():
2929
assert response.headers["X-BlueAPI-VERSION"] == __version__
3030

3131

32-
async def test_log_request_details():
32+
@pytest.mark.parametrize("path,level", [("/", "info"), ("/healthz", "debug")])
33+
async def test_log_request_details(path: str, level: str):
3334
with mock.patch("blueapi.service.main.LOGGER") as logger:
3435
app = FastAPI()
3536
app.middleware("http")(log_request_details)
3637

37-
@app.post("/")
38+
@app.post(path)
3839
async def root():
3940
return {"message": "Hello World"}
4041

4142
client = TestClient(app)
42-
response = client.post("/", content="foo")
43+
response = client.post(path, content="foo")
4344

4445
assert response.status_code == 200
45-
logger.debug.assert_called_once_with(
46-
"testclient:50000 POST /",
47-
extra={
48-
"request_body": b"foo",
49-
},
50-
)
51-
52-
logger.info.assert_called_once_with(
53-
"testclient:50000 POST / 200",
54-
extra={
55-
"request_body": b"foo",
56-
},
46+
log_level = getattr(logger, level)
47+
log_level.assert_has_calls(
48+
[
49+
call(
50+
f"testclient:50000 POST {path}",
51+
extra={
52+
"request_body": b"foo",
53+
},
54+
),
55+
call(
56+
f"testclient:50000 POST {path} 200",
57+
extra={
58+
"request_body": b"foo",
59+
},
60+
),
61+
]
5762
)
5863

5964

0 commit comments

Comments
 (0)