-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.py
More file actions
40 lines (32 loc) · 1.21 KB
/
logger.py
File metadata and controls
40 lines (32 loc) · 1.21 KB
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
import logging
import json
import uuid
import sys
from contextvars import ContextVar
request_id_var: ContextVar[str] = ContextVar("request_id", default="-")
class JSONFormatter(logging.Formatter):
"""Outputs each log record as a single JSON line."""
def format(self, record):
log_entry = {
"timestamp": self.formatTime(record, self.datefmt),
"level": record.levelname,
"logger": record.name,
"request_id": request_id_var.get("-"),
"message": record.getMessage(),
}
if record.exc_info and record.exc_info[0]:
log_entry["exception"] = self.formatException(record.exc_info)
return json.dumps(log_entry)
def get_logger(name: str) -> logging.Logger:
"""Return a logger configured with JSON output and request-id injection."""
logger = logging.getLogger(name)
if not logger.handlers:
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(JSONFormatter())
logger.addHandler(handler)
logger.setLevel(logging.INFO)
logger.propagate = False
return logger
def new_request_id() -> str:
"""Generate a short, readable request ID."""
return uuid.uuid4().hex[:12]