diff options
| author | Elizabeth Hunt <elizabeth@simponic.xyz> | 2024-08-20 17:34:18 -0700 |
|---|---|---|
| committer | Elizabeth Hunt <elizabeth@simponic.xyz> | 2024-08-20 17:34:18 -0700 |
| commit | b4140460014d1fc134df9127600f6da2346376e3 (patch) | |
| tree | 5bee11079f3ab9e3fd5d2a68cee483c2c33557f4 /kennel/middleware.py | |
| parent | 6f223d2462df5e08856fb83fd76773a8af33acf9 (diff) | |
| download | kennel.hatecomputers.club-b4140460014d1fc134df9127600f6da2346376e3.tar.gz kennel.hatecomputers.club-b4140460014d1fc134df9127600f6da2346376e3.zip | |
init commit with base ECS and Network System
Diffstat (limited to 'kennel/middleware.py')
| -rw-r--r-- | kennel/middleware.py | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/kennel/middleware.py b/kennel/middleware.py new file mode 100644 index 0000000..dddf63b --- /dev/null +++ b/kennel/middleware.py @@ -0,0 +1,30 @@ +import uuid +import structlog +from fastapi import Request, Response +from .app import app, logger + +@app.middleware("http") +async def logger_middleware(request: Request, call_next): + structlog.contextvars.clear_contextvars() + structlog.contextvars.bind_contextvars( + path=request.url.path, + method=request.method, + client_host=request.client.host, + request_id=str(uuid.uuid4()), + ) + response = await call_next(request) + + structlog.contextvars.bind_contextvars( + status_code=response.status_code, + ) + + # Exclude /healthcheck endpoint from producing logs + if request.url.path != "/healthcheck": + if 400 <= response.status_code < 500: + logger.warn("Client error") + elif response.status_code >= 500: + logger.error("Server error") + else: + logger.info("OK") + + return response |
