26 lines
788 B
Python
26 lines
788 B
Python
from __future__ import annotations
|
|
|
|
import json
|
|
import logging
|
|
from pathlib import Path
|
|
|
|
_CONFIG_PATH = Path(__file__).parent / "logconfig.json"
|
|
LOG_CONFIG = json.loads(_CONFIG_PATH.read_text())
|
|
|
|
|
|
class AccessLogFilter(logging.Filter):
|
|
"""Enrich uvicorn access records with structured fields for Loki.
|
|
|
|
Uvicorn emits access records whose ``args`` tuple is
|
|
``(client_addr, method, full_path, http_version, status_code)``.
|
|
"""
|
|
|
|
def filter(self, record: logging.LogRecord) -> bool:
|
|
args = record.args
|
|
if isinstance(args, tuple) and len(args) >= 5:
|
|
record.event = "http_request"
|
|
record.client_ip = args[0]
|
|
record.method = args[1]
|
|
record.path = args[2]
|
|
record.status = args[4]
|
|
return True
|