CoolFace
Apppublic

shiva-1993/transfer-learning-project

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
logging_utils.py39 linesDownload Raw Back to utils
1"""Structured logging + fail-loud config validation.2 3One configured logger factory so every module logs with consistent levels and4format to stdout (which Hugging Face Spaces captures). `require_env` makes the5app fail LOUDLY at startup if required config is missing, instead of dying6deep in a request handler later.7"""8 9from __future__ import annotations10 11import logging12import os13 14_FORMAT = "%(asctime)s | %(levelname)-7s | %(name)s | %(message)s"15_configured = False16 17 18def _configure_root() -> None:19    global _configured20    if _configured:21        return22    level = os.getenv("LOG_LEVEL", "INFO").upper()23    logging.basicConfig(level=level, format=_FORMAT)24    _configured = True25 26 27def get_logger(name: str) -> logging.Logger:28    _configure_root()29    return logging.getLogger(name)30 31 32def require_env(keys: list[str]) -> None:33    """Raise RuntimeError naming every missing required env var."""34    missing = [k for k in keys if not os.getenv(k)]35    if missing:36        raise RuntimeError(37            "Missing required environment variables: " + ", ".join(missing)38        )39