Deprecate Spider.log().

This commit is contained in:
Andrey Rakhmatullin 2026-07-14 21:50:09 +05:00
parent d8ba1571e7
commit bfc219d5b7
3 changed files with 14 additions and 7 deletions

View File

@ -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

View File

@ -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

View File

@ -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")