From 4155893c768cd484aad3fddcdd3caa4e149db0cf Mon Sep 17 00:00:00 2001 From: sparrowsurya Date: Sun, 25 Aug 2024 22:34:29 +0530 Subject: [PATCH] changed to json config file for logging and background async task now closes within the lifetime of app --- api/main.py | 3 ++- log_config.json | 41 +++++++++++++++++++++++++++++++++++++++++ log_config.yaml | 34 ---------------------------------- run.py | 12 ++++++++++++ 4 files changed, 55 insertions(+), 35 deletions(-) create mode 100644 log_config.json delete mode 100644 log_config.yaml create mode 100644 run.py diff --git a/api/main.py b/api/main.py index 566a005..63bb17b 100644 --- a/api/main.py +++ b/api/main.py @@ -19,9 +19,10 @@ async def lifespan(app: FastAPI): logger.info("Application lifespan started!") interval = float(get_settings().interval) db = SessionLocal() - asyncio.create_task(delete_expired_paste_task(interval, db)) + delete_task = asyncio.create_task(delete_expired_paste_task(interval, db)) yield db.close() + delete_task.cancel() logger.info("Application lifespan ended!") diff --git a/log_config.json b/log_config.json new file mode 100644 index 0000000..c32199d --- /dev/null +++ b/log_config.json @@ -0,0 +1,41 @@ +{ + "version": 1, + "disable_existing_loggers": false, + "formatters": { + "default": { + "format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s" + }, + "access": { + "format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s" + } + }, + "handlers": { + "default": { + "formatter": "default", + "class": "logging.StreamHandler", + "stream": "ext://sys.stderr" + }, + "access": { + "formatter": "access", + "class": "logging.StreamHandler", + "stream": "ext://sys.stdout" + } + }, + "loggers": { + "uvicorn.error": { + "level": "INFO", + "handlers": ["default"], + "propagate": false + }, + "uvicorn.access": { + "level": "INFO", + "handlers": ["access"], + "propagate": false + } + }, + "root": { + "level": "DEBUG", + "handlers": ["default"], + "propagate": false + } + } diff --git a/log_config.yaml b/log_config.yaml deleted file mode 100644 index 711aeef..0000000 --- a/log_config.yaml +++ /dev/null @@ -1,34 +0,0 @@ -version: 1 -disable_existing_loggers: False -formatters: - default: - # "()": uvicorn.logging.DefaultFormatter - format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s' - access: - # "()": uvicorn.logging.AccessFormatter - format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s' -handlers: - default: - formatter: default - class: logging.StreamHandler - stream: ext://sys.stderr - access: - formatter: access - class: logging.StreamHandler - stream: ext://sys.stdout -loggers: - uvicorn.error: - level: INFO - handlers: - - default - propagate: no - uvicorn.access: - level: INFO - handlers: - - access - propagate: no -root: - level: DEBUG - handlers: - - default - propagate: no \ No newline at end of file diff --git a/run.py b/run.py new file mode 100644 index 0000000..abf31f1 --- /dev/null +++ b/run.py @@ -0,0 +1,12 @@ +import uvicorn + +from api.main import app + + +def main(): + log_config_file = "log_config.json" + uvicorn.run(app, log_config=log_config_file) + + +if __name__ == "__main__": + main() \ No newline at end of file