Skip to content

Commit

Permalink
feat: add SpiderLoggerAdapter, change Spider.logger to return SpiderL…
Browse files Browse the repository at this point in the history
…oggerAdapter
  • Loading branch information
bloodforcream committed Apr 27, 2024
1 parent 5372c0b commit bd43233
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
13 changes: 7 additions & 6 deletions scrapy/utils/spider_logger_adapter.py
@@ -1,14 +1,15 @@
import logging
from typing import Any, Dict, Tuple
from typing import Any, MutableMapping, Tuple


class SpiderLoggerAdapter(logging.LoggerAdapter):
def process(self, msg: str, kwargs: Dict) -> Tuple[str, Dict[str, Any]]:
def process(
self, msg: str, kwargs: MutableMapping[str, Any]
) -> Tuple[str, MutableMapping[str, Any]]:
"""Method that augments logging with additional 'extra' data"""
extra = kwargs.get("extra")
if not isinstance(extra, dict):
kwargs["extra"] = self.extra
elif isinstance(self.extra, dict):
if isinstance(kwargs.get("extra"), MutableMapping):
kwargs["extra"].update(self.extra)
else:
kwargs["extra"] = self.extra

return msg, kwargs
14 changes: 2 additions & 12 deletions tests/test_spider_logger_adapter.py
@@ -1,5 +1,5 @@
import logging
from typing import Dict, Optional
from typing import Any, Dict, Mapping, MutableMapping

import pytest

Expand All @@ -19,16 +19,6 @@
{"extra": None},
{"extra": {"spider": "test"}},
),
(
None,
{"extra": {"log_extra": "info"}},
{"extra": {"log_extra": "info"}},
),
(
None,
{"extra": None},
{"extra": None},
),
(
{"spider": "test"},
{"extra": {"spider": "test2"}},
Expand All @@ -37,7 +27,7 @@
),
)
def test_spider_logger_adapter_process(
base_extra: Optional[Dict], log_extra: Dict, expected_extra: Dict
base_extra: Mapping[str, Any], log_extra: MutableMapping, expected_extra: Dict
):
logger = logging.getLogger("test")
spider_logger_adapter = SpiderLoggerAdapter(logger, base_extra)
Expand Down

0 comments on commit bd43233

Please sign in to comment.