Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 1 addition & 26 deletions src/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down
32 changes: 32 additions & 0 deletions src/api/logging_config.py
Original file line number Diff line number Diff line change
@@ -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}")