45 lines
1.5 KiB
Python
45 lines
1.5 KiB
Python
"""Structured logging.
|
|
|
|
JSON lines carrying ``job_id``, ``asset_id``, and ``operation_id`` when present,
|
|
so later stages can correlate work across the API, worker, and jobs. Secrets are
|
|
never passed to the logger; ``SecretStr`` masks them even if one slips through.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import logging
|
|
import sys
|
|
from datetime import datetime, timezone
|
|
|
|
CONTEXT_FIELDS = ("job_id", "asset_id", "operation_id")
|
|
|
|
|
|
class JsonFormatter(logging.Formatter):
|
|
def format(self, record: logging.LogRecord) -> str:
|
|
payload = {
|
|
"ts": datetime.fromtimestamp(record.created, timezone.utc).isoformat(),
|
|
"level": record.levelname,
|
|
"logger": record.name,
|
|
"message": record.getMessage(),
|
|
}
|
|
for field in CONTEXT_FIELDS:
|
|
value = getattr(record, field, None)
|
|
if value is not None:
|
|
payload[field] = value
|
|
if record.exc_info:
|
|
payload["exc"] = self.formatException(record.exc_info)
|
|
return json.dumps(payload, default=str)
|
|
|
|
|
|
def configure_logging(level: str = "INFO", fmt: str = "json") -> None:
|
|
handler = logging.StreamHandler(sys.stderr)
|
|
if fmt == "json":
|
|
handler.setFormatter(JsonFormatter())
|
|
else:
|
|
handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)s %(name)s %(message)s"))
|
|
root = logging.getLogger()
|
|
root.handlers.clear()
|
|
root.addHandler(handler)
|
|
root.setLevel(level.upper())
|