forked from browser-use/web-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo_logging.py
99 lines (87 loc) · 2.52 KB
/
demo_logging.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import asyncio
from src.utils.task_logging import (
TaskLogger, TaskStatus, ActionType, RetryConfig,
ColorScheme, SeparatorStyle
)
async def demo_logging():
# Initialize logger with custom styles
logger = TaskLogger(
"demo_task",
"Demonstrate all logging features",
color_scheme=ColorScheme(),
separator_style=SeparatorStyle(
task="★" * 40,
phase="•" * 30,
error="!" * 35
)
)
# Start navigation phase
logger.start_phase("Navigation Phase")
logger.update_step(
"Navigate to example.com",
TaskStatus.RUNNING,
action_type=ActionType.NAVIGATION,
context={"url": "https://example.com"}
)
# Update browser state
logger.update_browser_state(
url="https://example.com",
page_ready=True,
dynamic_content_loaded=True,
visible_elements=15,
page_title="Example Domain"
)
# Complete navigation
logger.update_step(
"Page loaded successfully",
TaskStatus.COMPLETE,
action_type=ActionType.NAVIGATION,
progress=0.25,
results={"status": 200, "load_time": 0.5}
)
# Start interaction phase
logger.start_phase("Interaction Phase")
logger.update_step(
"Click search button",
TaskStatus.RUNNING,
action_type=ActionType.INTERACTION,
context={"element": "search_button"}
)
# Simulate error and retry
async def failing_operation():
raise ValueError("Search button not found")
try:
await logger.execute_with_retry(
failing_operation,
"click_search",
RetryConfig(max_retries=2, base_delay=0.1)
)
except ValueError:
pass
# Start extraction phase
logger.start_phase("Data Extraction Phase")
logger.update_step(
"Extract search results",
TaskStatus.RUNNING,
action_type=ActionType.EXTRACTION,
progress=0.75
)
# Complete extraction
logger.update_step(
"Data extracted successfully",
TaskStatus.COMPLETE,
action_type=ActionType.EXTRACTION,
progress=1.0,
results={"items_found": 10}
)
# Display log history
print("\nLog History:")
print("=" * 80)
for entry in logger.get_log_history():
print(entry)
print("=" * 80)
# Log final state
print("\nFinal State:")
logger.log_state()
if __name__ == "__main__":
asyncio.run(demo_logging())