diff --git a/src/api/app.py b/src/api/app.py index c91e5fbc9..74a0a7b7f 100644 --- a/src/api/app.py +++ b/src/api/app.py @@ -7,13 +7,11 @@ """ -import logging -import os +import logging_config # noqa: F401 - Ensure logging is configured before other imports from contextlib import asynccontextmanager from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware -from dotenv import load_dotenv import uvicorn from agents.conversation_agent_factory import ConversationAgentFactory @@ -23,29 +21,6 @@ from api.api_routes import router as backend_router from api.history_routes import router as history_router -# Load environment variables -load_dotenv() - -# Configure logging -# Basic application logging (default: INFO level) -AZURE_BASIC_LOGGING_LEVEL = os.getenv("AZURE_BASIC_LOGGING_LEVEL", "INFO").upper() -# Azure package logging (default: WARNING level to suppress INFO) -AZURE_PACKAGE_LOGGING_LEVEL = os.getenv("AZURE_PACKAGE_LOGGING_LEVEL", "WARNING").upper() -# Azure logging packages (default: empty list) -AZURE_LOGGING_PACKAGES = [ - pkg.strip() for pkg in os.getenv("AZURE_LOGGING_PACKAGES", "").split(",") if pkg.strip() -] - -# Basic config: logging.basicConfig(level=logging.INFO) -logging.basicConfig( - level=getattr(logging, AZURE_BASIC_LOGGING_LEVEL, logging.INFO), - format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' -) - -# Package config: Azure loggers set to WARNING to suppress INFO -for logger_name in AZURE_LOGGING_PACKAGES: - logging.getLogger(logger_name).setLevel(getattr(logging, AZURE_PACKAGE_LOGGING_LEVEL, logging.WARNING)) - @asynccontextmanager async def lifespan(fastapi_app: FastAPI): diff --git a/src/api/logging_config.py b/src/api/logging_config.py new file mode 100644 index 000000000..2ee405995 --- /dev/null +++ b/src/api/logging_config.py @@ -0,0 +1,32 @@ +""" +Logging configuration module. +This module must be imported before any other modules to ensure proper logging setup. +""" + +import logging +import os +from dotenv import load_dotenv + +# Load environment variables first +load_dotenv() + +# Configure logging before any other imports +AZURE_BASIC_LOGGING_LEVEL = os.getenv("AZURE_BASIC_LOGGING_LEVEL", "INFO").upper() +AZURE_PACKAGE_LOGGING_LEVEL = os.getenv("AZURE_PACKAGE_LOGGING_LEVEL", "WARNING").upper() +AZURE_LOGGING_PACKAGES = [ + pkg.strip() for pkg in os.getenv("AZURE_LOGGING_PACKAGES", "").split(",") if pkg.strip() +] + +# Configure logging (this will be the first logging configuration) +logging.basicConfig( + level=getattr(logging, AZURE_BASIC_LOGGING_LEVEL, logging.INFO), + format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' +) + +# Configure Azure package loggers +for logger_name in AZURE_LOGGING_PACKAGES: + logging.getLogger(logger_name).setLevel(getattr(logging, AZURE_PACKAGE_LOGGING_LEVEL, logging.WARNING)) + +# Log that configuration is complete +logger = logging.getLogger(__name__) +logger.info(f"Logging configured - Basic: {AZURE_BASIC_LOGGING_LEVEL}, Azure packages: {AZURE_PACKAGE_LOGGING_LEVEL}, Packages: {AZURE_LOGGING_PACKAGES}")