Skip to content

Commit 5732394

Browse files
logging issue fix
1 parent 6ecaf4d commit 5732394

2 files changed

Lines changed: 33 additions & 30 deletions

File tree

src/api/app.py

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77
"""
88

99

10-
import logging
11-
import os
10+
import logging_config # Ensure logging is configured before other imports
1211
from contextlib import asynccontextmanager
1312
from fastapi import FastAPI
1413
from fastapi.middleware.cors import CORSMiddleware
1514

16-
from dotenv import load_dotenv
1715
import uvicorn
1816

1917
from agents.conversation_agent_factory import ConversationAgentFactory
@@ -23,33 +21,6 @@
2321
from api.api_routes import router as backend_router
2422
from api.history_routes import router as history_router
2523

26-
# Load environment variables
27-
load_dotenv()
28-
29-
# Configure logging
30-
# Basic application logging (default: INFO level)
31-
AZURE_BASIC_LOGGING_LEVEL = os.getenv("AZURE_BASIC_LOGGING_LEVEL", "INFO").upper()
32-
# Azure package logging (default: WARNING level to suppress INFO)
33-
AZURE_PACKAGE_LOGGING_LEVEL = os.getenv("AZURE_PACKAGE_LOGGING_LEVEL", "WARNING").upper()
34-
# Azure logging packages (default: empty list)
35-
AZURE_LOGGING_PACKAGES = [
36-
pkg.strip() for pkg in os.getenv("AZURE_LOGGING_PACKAGES", "").split(",") if pkg.strip()
37-
]
38-
39-
# Basic config: logging.basicConfig(level=logging.INFO)
40-
logging.basicConfig(
41-
level=getattr(logging, AZURE_BASIC_LOGGING_LEVEL, logging.INFO),
42-
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
43-
force=True # This ensures the configuration is applied
44-
)
45-
46-
# Package config: Azure loggers set to WARNING to suppress INFO
47-
for logger_name in AZURE_LOGGING_PACKAGES:
48-
logging.getLogger(logger_name).setLevel(getattr(logging, AZURE_PACKAGE_LOGGING_LEVEL, logging.WARNING))
49-
50-
logging.info(f"Logging configured - Basic: {AZURE_BASIC_LOGGING_LEVEL}, Azure packages: {AZURE_PACKAGE_LOGGING_LEVEL}, Packages: {AZURE_LOGGING_PACKAGES}")
51-
52-
5324
@asynccontextmanager
5425
async def lifespan(fastapi_app: FastAPI):
5526
"""

src/api/logging_config.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
Logging configuration module.
3+
This module must be imported before any other modules to ensure proper logging setup.
4+
"""
5+
6+
import logging
7+
import os
8+
from dotenv import load_dotenv
9+
10+
# Load environment variables first
11+
load_dotenv()
12+
13+
# Configure logging before any other imports
14+
AZURE_BASIC_LOGGING_LEVEL = os.getenv("AZURE_BASIC_LOGGING_LEVEL", "INFO").upper()
15+
AZURE_PACKAGE_LOGGING_LEVEL = os.getenv("AZURE_PACKAGE_LOGGING_LEVEL", "WARNING").upper()
16+
AZURE_LOGGING_PACKAGES = [
17+
pkg.strip() for pkg in os.getenv("AZURE_LOGGING_PACKAGES", "").split(",") if pkg.strip()
18+
]
19+
20+
# Configure logging (this will be the first logging configuration)
21+
logging.basicConfig(
22+
level=getattr(logging, AZURE_BASIC_LOGGING_LEVEL, logging.INFO),
23+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
24+
)
25+
26+
# Configure Azure package loggers
27+
for logger_name in AZURE_LOGGING_PACKAGES:
28+
logging.getLogger(logger_name).setLevel(getattr(logging, AZURE_PACKAGE_LOGGING_LEVEL, logging.WARNING))
29+
30+
# Log that configuration is complete
31+
logger = logging.getLogger(__name__)
32+
logger.info(f"Logging configured - Basic: {AZURE_BASIC_LOGGING_LEVEL}, Azure packages: {AZURE_PACKAGE_LOGGING_LEVEL}, Packages: {AZURE_LOGGING_PACKAGES}")

0 commit comments

Comments
 (0)