import logging import sys from pathlib import Path from typing import Optional def setup_logging(log_level: str = "INFO", log_file: Optional[Path] = None): """Configure logging for the application. Args: log_level: Logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL) log_file: Optional path to log file """ # Set up logging format log_format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s" # Configure logging level level = getattr(logging, log_level.upper()) # Configure root logger handlers = [logging.StreamHandler(sys.stdout)] # Add file handler if specified if log_file: log_file.parent.mkdir(parents=True, exist_ok=True) file_handler = logging.FileHandler(log_file) file_handler.setFormatter(logging.Formatter(log_format)) handlers.append(file_handler) # Configure logging logging.basicConfig( level=level, format=log_format, handlers=handlers ) # Set httpx logging to WARNING to reduce noise logging.getLogger("httpx").setLevel(logging.WARNING) # Set uvicorn access logs to WARNING to reduce noise logging.getLogger("uvicorn.access").setLevel(logging.WARNING) # Log startup message logging.info(f"Logging initialized at {log_level} level")