Skip to content

🕷️ The BEST Web Scraping framework that handles everything from a single request to a full-scale crawl! Don't be shy, join here: https://discord.gg/EMgGbDceNQ and follow here for daily tips and tricks: https://x.com/Scrapling_dev

feat: Change logger to be flexible enough for the spiders classes

Karim shoairaa4817f34969
diff --git a/scrapling/core/utils/__init__.py b/scrapling/core/utils/__init__.py
index 6ae80fe..77766c3 100644
--- a/scrapling/core/utils/__init__.py
+++ b/scrapling/core/utils/__init__.py
@@ -1,5 +1,7 @@
from ._utils import (
log,
+ set_logger,
+ reset_logger,
__CONSECUTIVE_SPACES_REGEX__,
flatten,
_is_iterable,
diff --git a/scrapling/core/utils/_utils.py b/scrapling/core/utils/_utils.py
index 2f57cfa..a2fa6ca 100644
--- a/scrapling/core/utils/_utils.py
+++ b/scrapling/core/utils/_utils.py
@@ -1,6 +1,7 @@
import logging
from itertools import chain
from re import compile as re_compile
+from contextvars import ContextVar, Token
from lxml import html
@@ -36,7 +37,25 @@ def setup_logger():
return logger
-log = setup_logger()
+_current_logger: ContextVar[logging.Logger] = ContextVar("scrapling_logger", default=setup_logger())
+
+
+class LoggerProxy:
+ def __getattr__(self, name: str):
+ return getattr(_current_logger.get(), name)
+
+
+log = LoggerProxy()
+
+
+def set_logger(logger: logging.Logger) -> Token:
+ """Set the current context logger. Returns token for reset."""
+ return _current_logger.set(logger)
+
+
+def reset_logger(token: Token) -> None:
+ """Reset logger to previous state using token."""
+ _current_logger.reset(token)
def flatten(lst: Iterable[Any]) -> List[Any]: