Skip to content

Commit 7165054

Browse files
committed
separate logging from utils
1 parent 944935d commit 7165054

File tree

6 files changed

+641
-640
lines changed

6 files changed

+641
-640
lines changed

stagehand/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from .client import Stagehand
33
from .config import StagehandConfig, default_config
44
from .handlers.observe_handler import ObserveHandler
5+
from .logging import configure_logging
56
from .metrics import StagehandFunctionName, StagehandMetrics
67
from .page import StagehandPage
78
from .schemas import (
@@ -17,7 +18,6 @@
1718
ObserveOptions,
1819
ObserveResult,
1920
)
20-
from .utils import configure_logging
2121

2222
__version__ = "0.0.1"
2323

stagehand/a11y/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
if TYPE_CHECKING:
88
from stagehand.page import StagehandPage
99

10+
from ..logging import StagehandLogger
1011
from ..types.a11y import (
1112
AccessibilityNode,
1213
AXNode,
1314
CDPSession,
1415
TreeResult,
1516
)
16-
from ..utils import StagehandLogger, format_simplified_tree
17+
from ..utils import format_simplified_tree
1718

1819

1920
async def _clean_structural_nodes(

stagehand/client.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import json
33
import os
44
import shutil
5-
import tempfile
65
import signal
76
import sys
7+
import tempfile
88
import time
99
from pathlib import Path
1010
from typing import Any, Literal, Optional
@@ -23,13 +23,12 @@
2323
from .config import StagehandConfig, default_config
2424
from .context import StagehandContext
2525
from .llm import LLMClient
26+
from .logging import StagehandLogger, default_log_handler
2627
from .metrics import StagehandFunctionName, StagehandMetrics
2728
from .page import StagehandPage
2829
from .schemas import AgentConfig
2930
from .utils import (
30-
StagehandLogger,
3131
convert_dict_keys_to_camel_case,
32-
default_log_handler,
3332
make_serializable,
3433
)
3534

@@ -47,7 +46,7 @@ class Stagehand:
4746

4847
# Dictionary to store one lock per session_id
4948
_session_locks = {}
50-
49+
5150
# Flag to track if cleanup has been called
5251
_cleanup_called = False
5352

@@ -193,7 +192,7 @@ def __init__(
193192
raise ValueError(
194193
"browserbase_project_id is required for BROWSERBASE env with existing session_id (or set BROWSERBASE_PROJECT_ID in env)."
195194
)
196-
195+
197196
# Register signal handlers for graceful shutdown
198197
self._register_signal_handlers()
199198

@@ -224,13 +223,16 @@ def __init__(
224223

225224
def _register_signal_handlers(self):
226225
"""Register signal handlers for SIGINT and SIGTERM to ensure proper cleanup."""
226+
227227
def cleanup_handler(sig, frame):
228228
# Prevent multiple cleanup calls
229229
if self.__class__._cleanup_called:
230230
return
231231

232232
self.__class__._cleanup_called = True
233-
print(f"\n[{signal.Signals(sig).name}] received. Ending Browserbase session...")
233+
print(
234+
f"\n[{signal.Signals(sig).name}] received. Ending Browserbase session..."
235+
)
234236

235237
try:
236238
# Try to get the current event loop
@@ -252,11 +254,11 @@ def cleanup_handler(sig, frame):
252254
def schedule_cleanup():
253255
task = asyncio.create_task(self._async_cleanup())
254256
# Shield the task to prevent it from being cancelled
255-
shielded = asyncio.shield(task)
257+
asyncio.shield(task)
256258
# We don't need to await here since we're in call_soon_threadsafe
257-
259+
258260
loop.call_soon_threadsafe(schedule_cleanup)
259-
261+
260262
except Exception as e:
261263
print(f"Error during signal cleanup: {str(e)}")
262264
sys.exit(1)

0 commit comments

Comments
 (0)