From 4aa38d163f9bdec98ced4e522ced3fd32bc6f4b7 Mon Sep 17 00:00:00 2001 From: HAL9000 Date: Fri, 24 Apr 2026 10:44:56 +0000 Subject: [PATCH] fix(events): add missing structlog import and use capture_logs in step file Replace the custom logging.Handler approach in step_when_emit_event with structlog.testing.capture_logs() for consistency with the stream error step, and add the missing import structlog that caused the lint failure. ISSUES CLOSED: #988 --- .../tdd_event_bus_exception_swallow_steps.py | 46 ++----------------- 1 file changed, 3 insertions(+), 43 deletions(-) diff --git a/features/steps/tdd_event_bus_exception_swallow_steps.py b/features/steps/tdd_event_bus_exception_swallow_steps.py index d19c8dcdd..66d34e049 100644 --- a/features/steps/tdd_event_bus_exception_swallow_steps.py +++ b/features/steps/tdd_event_bus_exception_swallow_steps.py @@ -11,8 +11,7 @@ and all scenarios now run as normal regression guards. from __future__ import annotations -import logging - +import structlog from behave import given, then, when # type: ignore[import-untyped] from behave.runner import Context # type: ignore[import-untyped] @@ -39,48 +38,9 @@ def step_given_bus_with_failing_handler(context: Context) -> None: @when("I emit an event that triggers the failing handler") def step_when_emit_event(context: Context) -> None: """Emit a PLAN_CREATED event and capture structlog output.""" - # Use Python logging to capture structlog output - # Create a handler that captures log records - logger = logging.getLogger("cleveragents.infrastructure.events.reactive") - - # Create a handler that captures logs - class StructlogCapturingHandler(logging.Handler): - def __init__(self): - super().__init__() - self.records = [] - - def emit(self, record): - self.records.append(record) - - handler = StructlogCapturingHandler() - handler.setLevel(logging.DEBUG) - logger.addHandler(handler) - - try: - # Emit the event + with structlog.testing.capture_logs() as captured: context.bus.emit(DomainEvent(event_type=EventType.PLAN_CREATED)) - - # Convert logging records to structlog format - context.captured_logs = [] - for record in handler.records: - # structlog with stdlib integration stores the dict in record.msg - log_entry = {} - - # When structlog is configured with stdlib.ProcessorFormatter, - # the structured data is stored directly in record.msg as a dict - if isinstance(record.msg, dict): - log_entry = record.msg - else: - # Fallback: ensure we at least have log_level - log_entry = {"event": record.getMessage()} - - # Ensure we have the log_level - if "log_level" not in log_entry: - log_entry["log_level"] = record.levelname.lower() - - context.captured_logs.append(log_entry) - finally: - logger.removeHandler(handler) + context.captured_logs = captured @then("the warning log should contain the exception message text")