-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Lucas Puerari
committed
Sep 5, 2023
1 parent
3013759
commit 2346531
Showing
5 changed files
with
30 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,32 @@ | ||
import time | ||
from starlette.middleware.base import BaseHTTPMiddleware | ||
|
||
from src.utils.logger import logger | ||
|
||
|
||
class LoggerMiddleware(BaseHTTPMiddleware): | ||
""" | ||
Middleware to add the logger to the request | ||
Middleware to add the logger to the request and logs request info | ||
""" | ||
|
||
async def dispatch(self, request, call_next): | ||
excluded_paths = [ | ||
'/-/ready', | ||
'/-/healthz', | ||
'/-/check-up', | ||
] | ||
|
||
if request.url.path not in excluded_paths: | ||
logger.debug(f"{request.method} {request.url.path}") | ||
start_time = time.time() | ||
|
||
request.state.logger = logger | ||
response = await call_next(request) | ||
|
||
if request.url.path not in excluded_paths: | ||
duration = time.time() - start_time | ||
logger.debug( | ||
f"{request.method} {request.url.path} {response.status_code} {duration:.6f}s" | ||
) | ||
|
||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters