import logging
import sys

from app.config import LoggingConfig


def configure_logging(name: str, cfg: LoggingConfig) -> None:
    handlers = [logging.StreamHandler(sys.stderr)]
    if cfg.file:
        handlers.append(logging.FileHandler(cfg.file, encoding="utf-8"))
    logging.basicConfig(
        level=getattr(logging, cfg.level.upper(), logging.INFO),
        format=f"%(asctime)s [{name}] %(levelname)s %(message)s",
        handlers=handlers,
        force=True,
    )
