Skip to content

Commit

Permalink
Implement ZyteLogFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
Gallaecio committed Dec 30, 2024
1 parent d214151 commit 0e3bed5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
17 changes: 17 additions & 0 deletions zyte_common_items/log_formatters.py
Original file line number Diff line number Diff line change
@@ -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
10 changes: 2 additions & 8 deletions zyte_common_items/pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 0e3bed5

Please sign in to comment.