File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # Getting started with the Sentry SDK for Python
2+
3+ import sentry_sdk
4+ sentry_sdk.init(dsn="https://foo@sentry.io/123")
5+
6+ After initialization, you can capture exceptions like this:
7+
8+ sentry_sdk.capture_exception(ValueError())
9+
10+ try:
11+ raise ValueError()
12+ except Exception:
13+ sentry_sdk.capture_exception()
14+
15+ You can create a scope to prepare data to attach to an event:
16+
17+ with sentry_sdk.get_current_hub().push_scope():
18+ with sentry_sdk.configure_scope() as scope:
19+ scope.transaction = "my_view_name"
20+ scope.set_tag("key", "value")
21+ scope.user = {"id": 123}
22+
23+ # ValueError event will have all that data attached
24+ capture_exception(ValueError())
25+
26+ # This one not since it is outside of the context manager
27+ capture_exception(ValueError())
28+
29+
30+ Scopes can be nested. If you call `` push_scope `` inside of the
31+ `` with `` -statement again, that scope will be pushed onto a stack. It will also
32+ inherit all data from the outer scope.
33+
34+ Head over to the other files to check out integrations, which use these
35+ low-level APIs so you don't have to.
Original file line number Diff line number Diff line change 1+ # Using Flask with Celery
2+
3+ Just add `` CeleryIntegration() `` to your `` integrations `` array. For example, in Django:
4+
5+ from sentry_sdk.integrations.celery import CeleryIntegration
6+ from sentry_sdk.integrations.django import DjangoIntegration
7+ from sentry_sdk import init
8+
9+ init(dsn="https://foo@sentry.io/123", integrations=[DjangoIntegration(), CeleryIntegration()])
Original file line number Diff line number Diff line change 1+ # Using Sentry with Django
2+
3+ In your `` settings.py `` :
4+
5+ from sentry_sdk.integrations.django import DjangoIntegration
6+ from sentry_sdk import init
7+
8+ init(dsn="https://foo@sentry.io/123", integrations=[DjangoIntegration()])
9+
10+ * All exceptions are reported.
11+
12+ * Request data is attached to your events.
13+
14+ * The current user ID, email and username from `` django.contrib.auth `` is
15+ attached to your events.
16+
17+ * Logging with any logger will create breadcrumbs. See logging docs for more
18+ information.
Original file line number Diff line number Diff line change 1+ # Using Sentry with Flask
2+
3+ from sentry_sdk.integrations.flask import FlaskIntegration
4+ from sentry_sdk import init
5+
6+ init(dsn="https://foo@sentry.io/123", integrations=[FlaskIntegration()])
7+
8+ app = Flask(__name__)
9+
10+
11+ * You can actually run that ` init ` anywhere. Before or after you define your
12+ routes, before or after you register extensions.
13+
14+ * The Flask integration will be installed for all of your apps. It hooks into
15+ Flask's signals, not anything on the app object.
16+
17+ * Request data is attached to all events.
18+
19+ * All exceptions leading to a Internal Server Error are reported.
20+
21+ * Logging with ` app.logger ` or really * any* logger will create breadcrumbs. See
22+ logging docs for more information.
Original file line number Diff line number Diff line change 1+ # Using Sentry with stdlib logging
2+
3+ Calling `` sentry_sdk.init() `` already captures any logging message with a level
4+ higher than or equal to `` INFO `` . You can change this behavior by explicitly
5+ passing the logging integration like any other:
6+
7+ from sentry_sdk.integrations.logging import LoggingIntegration
8+ from sentry_sdk import init
9+
10+ sentry_logging = LoggingIntegration(
11+ level=logging.DEBUG,
12+ event_level=logging.ERROR
13+ )
14+ init(dsn="https://foo@sentry.io/123", integrations=[sentry_logging])
15+
16+ The above configuration captures * all* messages, now including `` DEBUG `` , as
17+ breadcrumbs. Messages with level `` ERROR `` and above will show up as their own
18+ events as well.
19+
20+ ## Config
21+
22+ * `` level `` (default `` INFO `` ): Log records with a level higher than or equal
23+ to `` level `` will be recorded as breadcrumbs. Any log record with a level
24+ lower than this one is completely ignored.
25+
26+ * `` event_level `` (default `` None `` ): Log records with a level higher than or
27+ equal to `` event_level `` will additionally be reported as event. A value of
28+ `` None `` means that no log records will be sent as events.
You can’t perform that action at this time.
0 commit comments