From 0e3bed59b7e4cc9e4ef3ea1c223f271ed4b08aed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Chaves?= Date: Mon, 30 Dec 2024 14:01:38 +0100 Subject: [PATCH] Implement ZyteLogFormatter --- zyte_common_items/log_formatters.py | 17 +++++++++++++++++ zyte_common_items/pipelines.py | 10 ++-------- 2 files changed, 19 insertions(+), 8 deletions(-) create mode 100644 zyte_common_items/log_formatters.py diff --git a/zyte_common_items/log_formatters.py b/zyte_common_items/log_formatters.py new file mode 100644 index 0000000..863b5f3 --- /dev/null +++ b/zyte_common_items/log_formatters.py @@ -0,0 +1,17 @@ +from logging import INFO + +from scrapy.exceptions import DropItem +from scrapy.logformatter import LogFormatter + + +class InfoDropItem(DropItem): + """DropItem subclass for items that should be dropped with an INFO message + (instead of the default WARNING message).""" + + +class ZyteLogFormatter(LogFormatter): + def dropped(self, item, exception, response, spider): + data = super().dropped(item, exception, response, spider) + if isinstance(exception, InfoDropItem): + data["level"] = INFO + return data diff --git a/zyte_common_items/pipelines.py b/zyte_common_items/pipelines.py index ed564aa..64dc225 100644 --- a/zyte_common_items/pipelines.py +++ b/zyte_common_items/pipelines.py @@ -4,9 +4,8 @@ from copy import deepcopy from warnings import warn -from scrapy.exceptions import DropItem - -from zyte_common_items import ae +from .ae import ae +from .log_formatters import InfoDropItem class AEPipeline: @@ -61,11 +60,6 @@ def process_item(self, item, spider): return ae.downgrade(item) -class InfoDropItem(DropItem): - """DropItem subclass for items that should be dropped with an INFO message - (instead of the default WARNING message).""" - - class DropLowProbabilityItemPipeline: """This pipeline drops an item if its probability, defined in the settings, is less than the specified threshold.