-
Notifications
You must be signed in to change notification settings - Fork 77
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
nik
committed
Feb 19, 2024
1 parent
e17a2a1
commit 88c84f4
Showing
11 changed files
with
114 additions
and
58 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
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import json | ||
import logging | ||
from logging import Formatter | ||
from starlette.middleware.base import BaseHTTPMiddleware | ||
|
||
|
||
class JsonFormatter(Formatter): | ||
def __init__(self): | ||
super(JsonFormatter, self).__init__() | ||
|
||
def format(self, record): | ||
json_record = {} | ||
json_record["message"] = record.getMessage() | ||
if "url" in record.__dict__: | ||
json_record["url"] = record.__dict__["url"] | ||
if "method" in record.__dict__: | ||
json_record["method"] = record.__dict__["method"] | ||
if "status_code" in record.__dict__: | ||
json_record["status_code"] = record.__dict__["status_code"] | ||
return json.dumps(json_record) | ||
|
||
|
||
logger = logging.root | ||
handler = logging.StreamHandler() | ||
handler.setFormatter(JsonFormatter()) | ||
logger.handlers = [handler] | ||
logger.setLevel(logging.DEBUG) | ||
logging.getLogger("uvicorn.access").disabled = True | ||
|
||
|
||
class LogMiddleware(BaseHTTPMiddleware): | ||
async def dispatch(self, request, call_next): | ||
response = await call_next(request) | ||
logger.info( | ||
"Request", | ||
extra={ | ||
"method": request.method, | ||
"url": str(request.url), | ||
"status_code": response.status_code | ||
}, | ||
) | ||
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
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