From 3a7127c720ed136cdce1dfd5ef08b4eabf5415de Mon Sep 17 00:00:00 2001 From: demo-bot Date: Sun, 9 Aug 2026 16:03:46 +0000 Subject: [PATCH] decomposer: generate deliverable files for Define the service contract and project architecture for the FastAPI endpoint monitoring service.; Implement the typed monitor CRUD API and concurrency-safe in-memory state according to the service design.; Implement secure on-demand endpoint checks with status updates, latency measurement, robust error handling, and redacted structured logs.; Add operational API endpoints and environment-driven runtime configuration to the monitoring service.; Create automated tests for the monitoring service.; Package the service with Docker and developer documentation.; Validate the complete project. --- app/logging_config.py | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/app/logging_config.py b/app/logging_config.py index 9766009..2c3253c 100644 --- a/app/logging_config.py +++ b/app/logging_config.py @@ -1,26 +1,41 @@ import json import logging -from datetime import UTC, datetime +from datetime import datetime, timezone +from urllib.parse import urlsplit, urlunsplit -_STANDARD = set(logging.makeLogRecord({}).__dict__) | {"message", "asctime"} + +SAFE_EXTRA = ("monitor_id", "url", "status", "http_status", "latency_ms", "error") + + +def redact_url(value: str) -> str: + try: + parts = urlsplit(value) + host = parts.hostname or "" + if parts.port: + host = f"{host}:{parts.port}" + return urlunsplit((parts.scheme, host, parts.path, "", "")) + except (TypeError, ValueError): + return "[invalid-url]" class JsonFormatter(logging.Formatter): def format(self, record: logging.LogRecord) -> str: payload: dict[str, object] = { - "timestamp": datetime.now(UTC).isoformat(), + "timestamp": datetime.now(timezone.utc).isoformat(), "level": record.levelname, + "logger": record.name, "event": record.getMessage(), } - for key, value in record.__dict__.items(): - if key not in _STANDARD and key not in {"args", "msg"}: - payload[key] = value - return json.dumps(payload, default=str, separators=(",", ":")) + for key in SAFE_EXTRA: + if hasattr(record, key): + value = getattr(record, key) + payload[key] = redact_url(str(value)) if key == "url" else value + return json.dumps(payload, separators=(",", ":"), default=str) def configure_logging(level: str) -> None: handler = logging.StreamHandler() handler.setFormatter(JsonFormatter()) root = logging.getLogger() - root.handlers[:] = [handler] + root.handlers = [handler] root.setLevel(level.upper())