From c06b18a0142bd65a078292af5fb0c502d89befa2 Mon Sep 17 00:00:00 2001 From: RUiNtheExtinct Date: Sun, 28 Dec 2025 13:31:45 +0530 Subject: [PATCH] fix(logger): clear logs after all callbacks, not inside loop Move _logs_since_flush reset outside the callback loop so all registered callbacks receive the same log data instead of only the first callback getting logs while subsequent ones get an empty list. Add test to verify multiple callbacks all receive the same logs. --- app/logger.py | 2 +- tests-unit/app_test/test_logger.py | 40 ++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/app/logger.py b/app/logger.py index 2a57911c7..da7ff908d 100644 --- a/app/logger.py +++ b/app/logger.py @@ -41,7 +41,7 @@ class LogInterceptor(io.TextIOWrapper): raise for cb in self._flush_callbacks: cb(self._logs_since_flush) - self._logs_since_flush = [] + self._logs_since_flush = [] def on_flush(self, callback): self._flush_callbacks.append(callback) diff --git a/tests-unit/app_test/test_logger.py b/tests-unit/app_test/test_logger.py index 17e120a33..e749c2a2a 100644 --- a/tests-unit/app_test/test_logger.py +++ b/tests-unit/app_test/test_logger.py @@ -92,6 +92,46 @@ class TestLogInterceptorFlush: # Logs should be cleared assert interceptor._logs_since_flush == [] + def test_flush_multiple_callbacks_receive_same_logs(self): + """Test that all callbacks receive the same logs, not just the first one.""" + from app.logger import LogInterceptor + + class MockStream: + def __init__(self): + self._buffer = io.BytesIO() + self.encoding = 'utf-8' + self.line_buffering = False + + @property + def buffer(self): + return self._buffer + + mock_stream = MockStream() + interceptor = LogInterceptor(mock_stream) + + # Register multiple callbacks + callback1_results = [] + callback2_results = [] + callback3_results = [] + interceptor.on_flush(lambda logs: callback1_results.append(len(logs))) + interceptor.on_flush(lambda logs: callback2_results.append(len(logs))) + interceptor.on_flush(lambda logs: callback3_results.append(len(logs))) + + # Add some logs + interceptor._logs_since_flush = [ + {"t": "test", "m": "message1"}, + {"t": "test", "m": "message2"}, + {"t": "test", "m": "message3"} + ] + + # Flush should execute all callbacks with the same logs + interceptor.flush() + + # All callbacks should have received 3 log entries + assert callback1_results == [3] + assert callback2_results == [3] + assert callback3_results == [3] + class TestLogInterceptorWrite: """Test that LogInterceptor.write() works correctly."""