Skip to content

Instantly share code, notes, and snippets.

@ric2b
Forked from Framartin/extensions.py
Last active March 28, 2021 01:35
Show Gist options
  • Save ric2b/a0f27478d03a9a617e8f3043e2070cb0 to your computer and use it in GitHub Desktop.
Save ric2b/a0f27478d03a9a617e8f3043e2070cb0 to your computer and use it in GitHub Desktop.
Simplest scrapy extension to send all errors and exceptions to Sentry
import sentry_sdk
import os
from scrapy.exceptions import NotConfigured
class SentryLogging(object):
"""
Send exceptions and errors to Sentry.
"""
@classmethod
def from_crawler(cls, crawler):
if os.getenv('SENTRY_DSN', None) is None:
raise NotConfigured
sentry_sdk.init()
# return the extension object
return cls()
# Add the following lines to your settings.py file:
# ...
# Enable or disable extensions
# See https://doc.scrapy.org/en/latest/topics/extensions.html
EXTENSIONS = {
'myproject.extensions.SentryLogging': -1, # Load SentryLogging extension before others
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment