import os
import warnings
import logging
import defines
def _setup_logging(level=defines.logging_level) -> logging.Logger:
    os.environ["TORCH_CPP_LOG_LEVEL"] = "ERROR"
    warnings.filterwarnings(
        "ignore", message="Overriding a previously registered kernel"
    )
    warnings.filterwarnings("ignore", message="Warning only once for all operators")
    warnings.filterwarnings("ignore", message=".*Couldn't find ffmpeg or avconv.*")
    warnings.filterwarnings("ignore", message="'force_all_finite' was renamed to")
    warnings.filterwarnings("ignore", message="n_jobs value 1 overridden")
    warnings.filterwarnings("ignore", message=".*websocket.*is deprecated")

    numeric_level = getattr(logging, level.upper(), None)
    if not isinstance(numeric_level, int):
        raise ValueError(f"Invalid log level: {level}")

    # Create a custom formatter
    formatter = logging.Formatter(
        fmt="%(levelname)s - %(filename)s:%(lineno)d - %(message)s",
        datefmt="%Y-%m-%d %H:%M:%S"
    )

    # Create a handler (e.g., StreamHandler for console output)
    handler = logging.StreamHandler()
    handler.setFormatter(formatter)

    # Configure root logger
    logging.basicConfig(
        level=numeric_level,
        handlers=[handler],  # Use only your handler
        force=True,
    )

    # Set levels for noisy loggers
    for noisy_logger in (
        "uvicorn",
        "uvicorn.error",
        "uvicorn.access",
        "fastapi",
        "starlette",
    ):
        logger = logging.getLogger(noisy_logger)
        logger.setLevel(logging.WARNING)
        logger.handlers = []  # Remove default handlers
        logger.addHandler(handler)  # Add your custom handler

    logger = logging.getLogger(__name__)
    return logger

logger = _setup_logging(level=defines.logging_level)
logger.debug(f"Logging initialized with level: {defines.logging_level}")