api / core /logging.py
Codebuff
Fix Kick streams by making every rung prove it produced bytes
4144c35
Raw History Blame Contribute Delete
1.17 kB
"""Logging + request tracking.
Everything goes to stdout so it shows up in the Hugging Face Space runtime
logs. The previous version appended to /app/requests.jsonl, which is ephemeral
on a Space and grows without bound.
"""
from __future__ import annotations
import logging
import os
import sys
_LEVEL = os.environ.get("LOG_LEVEL", "INFO").upper()
def setup_logging() -> logging.Logger:
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(
logging.Formatter("%(asctime)s %(levelname)s [%(name)s] %(message)s")
)
root = logging.getLogger()
root.handlers = [handler]
root.setLevel(_LEVEL)
logger = logging.getLogger("vod")
logger.setLevel(_LEVEL)
return logger
log = setup_logging()
def _clean(value: str, limit: int = 240) -> str:
return " ".join(str(value or "").split())[:limit]
def track(action: str, url: str = "", status: str = "", detail: str = "") -> None:
"""Emit one structured request event to stdout."""
log.info(
"event=%s status=%s detail=%s url=%s",
_clean(action, 40),
_clean(status, 60),
_clean(detail),
_clean(url, 300),
)