from __future__ import annotations

import logging
import os
import sys
from typing import Literal

LogLevel = Literal["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]


def setupLogging(
    *,
    appName: str,
    level: LogLevel | None = None,
) -> None:
    """
    Central logging setup for all gateway apps.

    Env overrides:
      LOG_LEVEL=DEBUG|INFO|...
    """
    logLevel = (level or os.getenv("LOG_LEVEL", "INFO")).upper()
    numericLevel = getattr(logging, logLevel, logging.INFO)

    format = "%(asctime)s [%(levelname)s] %(name)s: %(message)s"

    logging.basicConfig(
        level=numericLevel,
        format=format,
        stream=sys.stdout,
        force=True,
    )

    logging.getLogger().info("Logging configured app=%s level=%s", appName, logLevel)
