generated from kamikaze/python3-project-template
-
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.
passing logging config to uvicorn, json
- Loading branch information
Showing
2 changed files
with
56 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,38 @@ | ||
import logging.config | ||
|
||
import uvicorn | ||
|
||
from dataset_image_annotator.api.http import app | ||
from dataset_image_annotator.conf import settings | ||
|
||
|
||
if __name__ == '__main__': | ||
uvicorn.run(app, host=settings.service_addr, port=settings.service_port) | ||
LOGGING_CONFIG = { | ||
'version': 1, | ||
'disable_existing_loggers': False, | ||
'formatters': { | ||
'default': { | ||
'()': 'dataset_image_annotator.formatter.JSONFormatter', | ||
}, | ||
}, | ||
'handlers': { | ||
'default': { | ||
'level': settings.logging_level, | ||
'class': 'logging.StreamHandler', | ||
'stream': 'ext://sys.stdout', | ||
'formatter': 'default', | ||
}, | ||
}, | ||
'loggers': { | ||
'': { | ||
'handlers': ['default', ], | ||
}, | ||
'dataset_image_annotator': { | ||
'handlers': ['default', ], | ||
'level': settings.logging_level, | ||
'propagate': False, | ||
} | ||
} | ||
} | ||
logging.config.dictConfig(LOGGING_CONFIG) | ||
|
||
uvicorn.run(app, host=settings.service_addr, port=settings.service_port, proxy_headers=True, log_config=LOGGING_CONFIG) |
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,24 @@ | ||
import base64 | ||
import json | ||
import logging | ||
from datetime import datetime, date | ||
from socket import socket | ||
|
||
|
||
class CustomJSONEncoder(json.JSONEncoder): | ||
def default(self, o): | ||
if isinstance(o, datetime): | ||
return o.isoformat() | ||
elif isinstance(o, date): | ||
return o.isoformat() | ||
elif isinstance(o, bytes): | ||
return base64.b64encode(o).decode('ascii') | ||
elif isinstance(o, socket): | ||
return str(o) | ||
|
||
return json.JSONEncoder.default(self, o) | ||
|
||
|
||
class JSONFormatter(logging.Formatter): | ||
def format(self, record): | ||
return json.dumps(record.__dict__, cls=CustomJSONEncoder) |