diff --git a/README.md b/README.md index 1fce913..eff409f 100644 --- a/README.md +++ b/README.md @@ -401,6 +401,14 @@ def should_abort_request(request): PLAYWRIGHT_ABORT_REQUEST = should_abort_request ``` +### `PLAYWRIGHT_REQUEST_RESPONSE_LOGGER_ENABLED` +Type `bool`, default `True` + +Whether or not to emit debug-level log lines for Playwright request & responses. +```python +PLAYWRIGHT_REQUEST_RESPONSE_LOGGER_ENABLED = False +``` + ### General note about settings For settings that accept object paths as strings, passing callable objects is only supported when using Scrapy>=2.4. With prior versions, only strings are diff --git a/scrapy_playwright/handler.py b/scrapy_playwright/handler.py index 0363f2b..24ea0f3 100644 --- a/scrapy_playwright/handler.py +++ b/scrapy_playwright/handler.py @@ -97,6 +97,7 @@ class Config: restart_disconnected_browser: bool target_closed_max_retries: int = 3 use_threaded_loop: bool = False + request_response_logger_enabled: bool = True @classmethod def from_settings(cls, settings: Settings) -> "Config": @@ -122,6 +123,9 @@ def from_settings(cls, settings: Settings) -> "Config": ), use_threaded_loop=platform.system() == "Windows" or settings.getbool("_PLAYWRIGHT_THREADED_LOOP", False), + request_response_logger_enabled=settings.getbool( + "PLAYWRIGHT_REQUEST_RESPONSE_LOGGER_ENABLED", default=True + ), ) cfg.cdp_kwargs.pop("endpoint_url", None) cfg.connect_kwargs.pop("ws_endpoint", None) @@ -323,10 +327,11 @@ async def _create_page(self, request: Request, spider: Spider) -> Page: page.on("close", self._make_close_page_callback(context_name)) page.on("crash", self._make_close_page_callback(context_name)) - page.on("request", _make_request_logger(context_name, spider)) - page.on("response", _make_response_logger(context_name, spider)) page.on("request", self._increment_request_stats) page.on("response", self._increment_response_stats) + if self.config.request_response_logger_enabled: + page.on("request", _make_request_logger(context_name, spider)) + page.on("response", _make_response_logger(context_name, spider)) return page