This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
"""Structured logs: one JSON object per line on stdout.
|
||||
|
||||
Logs never contain message texts, request bodies, query strings or headers.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import sys
|
||||
import threading
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any
|
||||
|
||||
_lock = threading.Lock()
|
||||
|
||||
|
||||
def log(level: str, event: str, **fields: Any) -> None:
|
||||
record = {
|
||||
"ts": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
|
||||
"level": level,
|
||||
"event": event,
|
||||
**fields,
|
||||
}
|
||||
line = json.dumps(record, ensure_ascii=False, default=str)
|
||||
with _lock:
|
||||
sys.stdout.write(line + "\n")
|
||||
sys.stdout.flush()
|
||||
Reference in New Issue
Block a user