From 0862599e0fbc46057ac5285eb3252c979d24ba37 Mon Sep 17 00:00:00 2001 From: Jordan Shatford Date: Fri, 3 Nov 2023 13:00:36 +1100 Subject: [PATCH] feat(api): store logging output to file Signed-off-by: Jordan Shatford --- apps/api/.gitignore | 1 + apps/api/app/main.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/apps/api/.gitignore b/apps/api/.gitignore index 390794f3..7bab32ec 100644 --- a/apps/api/.gitignore +++ b/apps/api/.gitignore @@ -4,3 +4,4 @@ sessions/ .env .mypy_cache/ venv/ +*.log* diff --git a/apps/api/app/main.py b/apps/api/app/main.py index e03cbef1..e1860c7e 100644 --- a/apps/api/app/main.py +++ b/apps/api/app/main.py @@ -1,5 +1,7 @@ +import logging import os import tomllib +from logging.handlers import RotatingFileHandler from typing import Any from fastapi import FastAPI @@ -11,6 +13,22 @@ from .routers import session from .session import session_manager + +handler = RotatingFileHandler( + 'output.log', + maxBytes=1024 * 1024 * 5, # 5 MB + backupCount=5, +) +logging.basicConfig( + format='[%(asctime)s] %(levelname)s ' + + ' - %(name)s - %(module)s - %(funcName)s: %(message)s', + datefmt='%Y-%m-%d %H:%M:%S', + level=logging.WARNING, + handlers=[handler], +) +logging.getLogger('sse_starlette').setLevel(logging.ERROR) + + # Read data from pyproject.toml file pyproject_data: dict[str, Any] = {} pyproject_path = os.path.join(