Environment-variable configuration that fails fast, database-only health check, JSON request logs with a request id, security headers with a strict CSP, and a multi-stage non-root Docker image with compose file. Ticket 1.1. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Sk72AxLwQnYttF6VFSxQLt
50 lines
1.8 KiB
Python
50 lines
1.8 KiB
Python
"""JSON logs on stdout with a request id (AD-18)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import logging
|
|
from contextvars import ContextVar
|
|
from datetime import UTC, datetime
|
|
|
|
request_id_var: ContextVar[str] = ContextVar("request_id", default="-")
|
|
|
|
# Attributes set by logging itself; everything else passed via `extra` is emitted.
|
|
_STANDARD = set(logging.LogRecord("", 0, "", 0, "", (), None).__dict__) | {"message", "asctime"}
|
|
|
|
|
|
class JsonFormatter(logging.Formatter):
|
|
def format(self, record: logging.LogRecord) -> str:
|
|
payload: dict[str, object] = {
|
|
"ts": datetime.fromtimestamp(record.created, UTC).isoformat(timespec="milliseconds"),
|
|
"level": record.levelname,
|
|
"logger": record.name,
|
|
"message": record.getMessage(),
|
|
"request_id": getattr(record, "request_id", None) or request_id_var.get(),
|
|
}
|
|
for key, value in record.__dict__.items():
|
|
if key in _STANDARD or key in ("request_id", "request"):
|
|
continue
|
|
if value is None or isinstance(value, str | int | float | bool):
|
|
payload[key] = value
|
|
if record.exc_info:
|
|
payload["exception"] = self.formatException(record.exc_info)
|
|
return json.dumps(payload, default=str)
|
|
|
|
|
|
def build_logging(level: str) -> dict:
|
|
return {
|
|
"version": 1,
|
|
"disable_existing_loggers": False,
|
|
"formatters": {"json": {"()": "portal.config.logging.JsonFormatter"}},
|
|
"handlers": {
|
|
"stdout": {
|
|
"class": "logging.StreamHandler",
|
|
"stream": "ext://sys.stdout",
|
|
"formatter": "json",
|
|
}
|
|
},
|
|
"loggers": {"django.request": {"level": "ERROR"}},
|
|
"root": {"handlers": ["stdout"], "level": level},
|
|
}
|