31 lines
887 B
Python
31 lines
887 B
Python
from __future__ import annotations
|
|
|
|
import json
|
|
from datetime import datetime
|
|
from typing import Any, Dict, Optional
|
|
|
|
|
|
class StructuredLogger:
|
|
def __init__(self):
|
|
pass
|
|
|
|
def log(self, level: str, event: str, trace_id: str, payload: Optional[Dict[str, Any]] = None, error_code: Optional[str] = None) -> None:
|
|
print(json.dumps({
|
|
"trace_id": trace_id,
|
|
"level": level,
|
|
"event": event,
|
|
"error_code": error_code,
|
|
"payload": payload or {},
|
|
"created_at": datetime.now().isoformat(),
|
|
}, ensure_ascii=False))
|
|
|
|
|
|
_GLOBAL_STRUCTURED_LOGGER: Optional[StructuredLogger] = None
|
|
|
|
|
|
def get_structured_logger() -> StructuredLogger:
|
|
global _GLOBAL_STRUCTURED_LOGGER
|
|
if _GLOBAL_STRUCTURED_LOGGER is None:
|
|
_GLOBAL_STRUCTURED_LOGGER = StructuredLogger()
|
|
return _GLOBAL_STRUCTURED_LOGGER
|