diff --git a/aidial_analytics_realtime/app.py b/aidial_analytics_realtime/app.py index 2442492..f580647 100644 --- a/aidial_analytics_realtime/app.py +++ b/aidial_analytics_realtime/app.py @@ -19,21 +19,15 @@ from aidial_analytics_realtime.time import parse_time from aidial_analytics_realtime.topic_model import TopicModel from aidial_analytics_realtime.universal_api_utils import merge +from aidial_analytics_realtime.utils.log_config import configure_loggers, logger RATE_PATTERN = r"/v1/(.+?)/rate" CHAT_COMPLETION_PATTERN = r"/openai/deployments/(.+?)/chat/completions" EMBEDDING_PATTERN = r"/openai/deployments/(.+?)/embeddings" - app = FastAPI() -logging.basicConfig( - level=logging.INFO, - format="%(asctime)s [%(levelname)s] - %(message)s", - handlers=[logging.StreamHandler()], -) -logger = logging.getLogger(__name__) -logger.setLevel(logging.DEBUG) +configure_loggers() @app.on_event("startup") diff --git a/aidial_analytics_realtime/utils/log_config.py b/aidial_analytics_realtime/utils/log_config.py new file mode 100644 index 0000000..652a2f4 --- /dev/null +++ b/aidial_analytics_realtime/utils/log_config.py @@ -0,0 +1,39 @@ +import logging +import sys + +from uvicorn.logging import DefaultFormatter + +logger = logging.getLogger("app") + + +def configure_loggers(): + # Delegate uvicorn logs to the root logger + # to achieve uniform log formatting + for name, log in logging.getLogger().manager.loggerDict.items(): + if isinstance(log, logging.Logger) and name.startswith("uvicorn"): + log.handlers = [] + log.propagate = True + + # Setting up log levels + logger.setLevel(logging.DEBUG) + + # Configuring the root logger + root = logging.getLogger() + root.setLevel(logging.INFO) + + root_has_stderr_handler = any( + isinstance(handler, logging.StreamHandler) + and handler.stream == sys.stderr + for handler in root.handlers + ) + + # Do not override the existing stderr handlers + # if they are already configured + if not root_has_stderr_handler: + formatter = DefaultFormatter( + fmt="%(asctime)s [%(levelname)s] - %(message)s" + ) + + handler = logging.StreamHandler(sys.stderr) + handler.setFormatter(formatter) + root.addHandler(handler)