File size: 1,165 Bytes
4144c35
095d95d
4144c35
 
 
 
095d95d
4144c35
 
 
 
 
 
 
 
 
 
 
 
 
095d95d
4144c35
 
 
 
 
 
 
095d95d
 
 
4144c35
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""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),
    )