initial commit
This commit is contained in:
40
infrastructure/logger_setup.py
Normal file
40
infrastructure/logger_setup.py
Normal file
@@ -0,0 +1,40 @@
|
||||
import logging
|
||||
import sys
|
||||
from typing import Any
|
||||
|
||||
from domain.ports import EventBusPort
|
||||
|
||||
|
||||
def setup_logging() -> None:
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s | %(levelname)-7s | %(name)s | %(message)s",
|
||||
datefmt="%H:%M:%S",
|
||||
stream=sys.stdout,
|
||||
)
|
||||
|
||||
|
||||
def attach_event_logger(event_bus: EventBusPort) -> None:
|
||||
"""Podłącza monitorowanie zdarzeń API — zgodnie z lekcją:
|
||||
'warto zapisywać i monitorować wszystkie zdarzenia'."""
|
||||
|
||||
log = logging.getLogger("events")
|
||||
|
||||
def on_request(data: dict[str, Any]) -> None:
|
||||
log.info(">>> [%s] attempt=%d params=%s",
|
||||
data["action"], data["attempt"], data.get("params", {}))
|
||||
|
||||
def on_response(data: dict[str, Any]) -> None:
|
||||
log.info("<<< [%s] http=%d data=%s",
|
||||
data["action"], data["http_code"], data["data"])
|
||||
|
||||
def on_retry(data: dict[str, Any]) -> None:
|
||||
log.warning("... retry reason=%s delay=%.1fs", data["reason"], data["delay"])
|
||||
|
||||
def on_step(data: dict[str, Any]) -> None:
|
||||
log.info("=== STEP: %s", data.get("description", ""))
|
||||
|
||||
event_bus.on("api:request", on_request)
|
||||
event_bus.on("api:response", on_response)
|
||||
event_bus.on("api:retry", on_retry)
|
||||
event_bus.on("workflow:step", on_step)
|
||||
Reference in New Issue
Block a user