Skip to content

Commit

Permalink
💬 Improve logging (Kludex#230)
Browse files Browse the repository at this point in the history
*  Improve logging
  • Loading branch information
aminalaee authored and khamaileon committed Jan 13, 2024
1 parent 49fdae0 commit 1f89763
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
9 changes: 6 additions & 3 deletions mangum/protocols/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,6 @@ async def send(self, message: Message) -> None:
Awaited by the application to send ASGI `http` events.
"""
message_type = message["type"]
self.logger.info(
"%s: '%s' event received from application.", self.state, message_type
)

if (
self.state is HTTPCycleState.REQUEST
Expand Down Expand Up @@ -141,6 +138,12 @@ async def send(self, message: Message) -> None:
self.state = HTTPCycleState.COMPLETE
await self.app_queue.put({"type": "http.disconnect"})

self.logger.info(
"%s %s %s",
self.request.method,
self.request.path,
self.response.status,
)
else:
raise UnexpectedMessage(
f"{self.state}: Unexpected '{message_type}' event received."
Expand Down
30 changes: 30 additions & 0 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,3 +603,33 @@ async def app(scope, receive, send):
"vary": "Accept-Encoding",
}
assert response["body"] == base64.b64encode(brotli.compress(body.encode())).decode()


@pytest.mark.parametrize(
"mock_aws_api_gateway_event", [["GET", b"", None]], indirect=True
)
def test_http_logging(mock_aws_api_gateway_event, caplog) -> None:
async def app(scope, receive, send):
assert scope["type"] == "http"
await send(
{
"type": "http.response.start",
"status": 200,
"headers": [[b"content-type", b"text/plain; charset=utf-8"]],
}
)

await send({"type": "http.response.body", "body": b"Hello, world!"})

handler = Mangum(app, lifespan="off")
response = handler(mock_aws_api_gateway_event, {})

assert response == {
"statusCode": 200,
"isBase64Encoded": False,
"headers": {"content-type": "text/plain; charset=utf-8"},
"multiValueHeaders": {},
"body": "Hello, world!",
}

assert "GET /test/hello 200" in caplog.text

0 comments on commit 1f89763

Please sign in to comment.