Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

Deprecate Spider.log(). (#7739)

Andrey Rakhmatullina591d15c0458
diff --git a/docs/topics/spiders.rst b/docs/topics/spiders.rst
index 887da0a15..770640adf 100644
--- a/docs/topics/spiders.rst
+++ b/docs/topics/spiders.rst
@@ -208,12 +208,6 @@ scrapy.Spider
:param response: the response to parse
:type response: :class:`~scrapy.http.Response`
- .. method:: log(message, [level])
-
- Wrapper that sends a log message through the Spider's :attr:`logger`,
- kept for backward compatibility. For more information see
- :ref:`topics-logging-from-spiders`.
-
.. method:: closed(reason)
Called when the spider closes. This method provides a shortcut to
diff --git a/scrapy/spiders/__init__.py b/scrapy/spiders/__init__.py
index de527d04f..02dfa2ac6 100644
--- a/scrapy/spiders/__init__.py
+++ b/scrapy/spiders/__init__.py
@@ -7,9 +7,11 @@ See documentation in docs/topics/spiders.rst
from __future__ import annotations
import logging
+import warnings
from typing import TYPE_CHECKING, Any, cast
from scrapy import signals
+from scrapy.exceptions import ScrapyDeprecationWarning
from scrapy.http import Request, Response
from scrapy.utils.trackref import object_ref
from scrapy.utils.url import url_is_from_spider
@@ -66,6 +68,11 @@ class Spider(object_ref):
can use it directly (e.g. Spider.logger.info('msg')) or use any other
Python logger too.
"""
+ warnings.warn(
+ "Spider.log() is deprecated, use methods of Spider.logger instead.",
+ ScrapyDeprecationWarning,
+ stacklevel=2,
+ )
self.logger.log(level, message, **kw)
@classmethod
diff --git a/tests/test_spider.py b/tests/test_spider.py
index 526cc8f23..fba1ef984 100644
--- a/tests/test_spider.py
+++ b/tests/test_spider.py
@@ -8,6 +8,7 @@ from testfixtures import LogCapture
from scrapy import signals
from scrapy.crawler import Crawler
+from scrapy.exceptions import ScrapyDeprecationWarning
from scrapy.http import Response, TextResponse, XmlResponse
from scrapy.settings import Settings
from scrapy.spiders import CSVFeedSpider, Spider, XMLFeedSpider
@@ -116,7 +117,12 @@ class TestSpider:
def test_log(self):
spider = self.spider_class("example.com")
- with mock.patch("scrapy.spiders.Spider.logger") as mock_logger:
+ with (
+ mock.patch("scrapy.spiders.Spider.logger") as mock_logger,
+ pytest.warns(
+ ScrapyDeprecationWarning, match=r"Spider.log\(\) is deprecated"
+ ),
+ ):
spider.log("test log msg", "INFO")
mock_logger.log.assert_called_once_with("INFO", "test log msg")