Skip to content

Commit 2a6f2ec

Browse files
committed
Update split pattern to deal with multiple times redirected log
Update test data to properly match the reality with line endings
1 parent 2674cf2 commit 2a6f2ec

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

src/apify_client/clients/resource_clients/log.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ def __init__(self, to_logger: logging.Logger, *, from_start: bool = True) -> Non
232232
if self._force_propagate:
233233
to_logger.propagate = True
234234
self._stream_buffer = list[bytes]()
235-
self._split_marker = re.compile(rb'(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z)')
235+
self._split_marker = re.compile(rb'(?:\n|^)(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z)')
236236
self._relevancy_time_limit: datetime | None = None if from_start else datetime.now(tz=timezone.utc)
237237

238238
def _process_new_data(self, data: bytes) -> None:

tests/unit/test_logging.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,20 @@
2323
_MOCKED_ACTOR_LOGS = (
2424
b'2025-05-13T07:24:12.588Z ACTOR: Pulling Docker image of build.\n'
2525
b'2025-05-13T07:24:12.686Z ACTOR: Creating Docker container.\n'
26-
b'2025-05-13T07:24:12.745Z ACTOR: Starting Docker container.', # Several logs merged into one chunk
26+
b'2025-05-13T07:24:12.745Z ACTOR: Starting Docker container.\n', # Several logs merged into one chunk
2727
b'2025-05-13T07:26:14.132Z [apify] DEBUG \xc3', # Chunked log split in the middle of the multibyte character
28-
b'\xa1', # part 2
29-
b'2025-05-13T07:24:14.132Z [apify] INFO multiline \n log',
30-
b'2025-05-13T07:25:14.132Z [apify] WARNING some warning',
31-
b'2025-05-13T07:26:14.132Z [apify] DEBUG c',
28+
b'\xa1\n', # part 2
29+
b'2025-05-13T07:24:14.132Z [apify] INFO multiline \n log\n',
30+
b'2025-05-13T07:25:14.132Z [apify] WARNING some warning\n',
31+
b'2025-05-13T07:26:14.132Z [apify] DEBUG c\n',
3232
b'2025-05-13T0', # Chunked log that got split in the marker
33-
b'7:26:14.132Z [apify] DEBUG d' # part 2
33+
b'7:26:14.132Z [apify] DEBUG d\n' # part 2
3434
b'2025-05-13T07:26:14.132Z [apify] DEB', # Chunked log that got split outside of marker
35-
b'UG e', # part 2
35+
b'UG e\n', # part 2
36+
# Already redirected message
37+
b'2025-05-16T12:08:50.123Z [apify.redirect-logger-4U1oAnKau6jpzjUuA] -> 2025-05-16T12:08:49.840Z ACTOR: Pulling\n',
3638
)
39+
_CHUNKED_MESSAGES_COUNT = 3
3740

3841
_EXPECTED_MESSAGES_AND_LEVELS = (
3942
('2025-05-13T07:24:12.588Z ACTOR: Pulling Docker image of build.', logging.INFO),
@@ -45,6 +48,10 @@
4548
('2025-05-13T07:26:14.132Z [apify] DEBUG c', logging.DEBUG),
4649
('2025-05-13T07:26:14.132Z [apify] DEBUG d', logging.DEBUG),
4750
('2025-05-13T07:26:14.132Z [apify] DEBUG e', logging.DEBUG),
51+
(
52+
'2025-05-16T12:08:50.123Z [apify.redirect-logger-4U1oAnKau6jpzjUuA] -> 2025-05-16T12:08:49.840Z ACTOR: Pulling',
53+
logging.INFO,
54+
),
4855
)
4956

5057

@@ -127,7 +134,8 @@ def propagate_stream_logs() -> None:
127134

128135

129136
@pytest.mark.parametrize(
130-
('log_from_start', 'expected_log_count'), [(True, len(_EXPECTED_MESSAGES_AND_LEVELS)), (False, 6)]
137+
('log_from_start', 'expected_log_count'),
138+
[(True, len(_EXPECTED_MESSAGES_AND_LEVELS)), (False, len(_EXPECTED_MESSAGES_AND_LEVELS) - _CHUNKED_MESSAGES_COUNT)],
131139
)
132140
@respx.mock
133141
async def test_redirected_logs_async(
@@ -162,7 +170,8 @@ async def test_redirected_logs_async(
162170

163171

164172
@pytest.mark.parametrize(
165-
('log_from_start', 'expected_log_count'), [(True, len(_EXPECTED_MESSAGES_AND_LEVELS)), (False, 6)]
173+
('log_from_start', 'expected_log_count'),
174+
[(True, len(_EXPECTED_MESSAGES_AND_LEVELS)), (False, len(_EXPECTED_MESSAGES_AND_LEVELS) - _CHUNKED_MESSAGES_COUNT)],
166175
)
167176
@respx.mock
168177
def test_redirected_logs_sync(

0 commit comments

Comments
 (0)