-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathtest_actor_log.py
More file actions
74 lines (53 loc) · 2.43 KB
/
test_actor_log.py
File metadata and controls
74 lines (53 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from __future__ import annotations
from typing import TYPE_CHECKING
from apify import Actor
if TYPE_CHECKING:
from .conftest import MakeActorFunction, RunActorFunction
async def test_actor_logging(
make_actor: MakeActorFunction,
run_actor: RunActorFunction,
) -> None:
async def main() -> None:
import logging
from apify.log import logger
async with Actor:
logger.setLevel(logging.DEBUG)
# Test Actor.log
Actor.log.debug('Debug message')
Actor.log.info('Info message')
# Test logger
logger.warning('Warning message')
logger.error('Error message')
# Test that exception is logged with the traceback
try:
raise ValueError('Dummy ValueError')
except Exception:
Actor.log.exception('Exception message')
# Test multiline message being indented correctly
logger.info('Multi\nline\nlog\nmessage')
# Test that exception in Actor.main is logged with the traceback
raise RuntimeError('Dummy RuntimeError')
actor = await make_actor(label='actor-log', main_func=main)
run_result = await run_actor(actor)
assert run_result.status == 'FAILED'
run_log = await actor.last_run().log().get()
assert run_log is not None
run_log_lines = run_log.splitlines()
# Remove the datetime from the start of log lines
run_log_lines = [line[25:] for line in run_log_lines]
# Join all lines to make it easier to search for expected content
full_log = '\n'.join(run_log_lines)
# Verify expected log messages are present (order-independent checks)
assert '[apify] DEBUG Debug message' in full_log
assert '[apify] INFO Info message' in full_log
assert '[apify] WARN Warning message' in full_log
assert '[apify] ERROR Error message' in full_log
assert '[apify] ERROR Exception message' in full_log
assert 'ValueError: Dummy ValueError' in full_log
assert '[apify] INFO Multi' in full_log
assert '[apify] ERROR Actor failed with an exception' in full_log
assert 'RuntimeError: Dummy RuntimeError' in full_log
# Verify multiline log message is present
assert 'line\nlog\nmessage' in full_log or ('line' in full_log and 'log' in full_log and 'message' in full_log)
# Verify exit message is present
assert '[apify] INFO Exiting Actor ({"exit_code": 91})' in full_log