|
| 1 | +# Integration with Flask |
| 2 | + |
| 3 | +Easiest way to get started: |
| 4 | + |
| 5 | + from flask import Flask |
| 6 | + from sentry_sdk import capture_exception |
| 7 | + from sentry_sdk.integrations.flask import FlaskSentry |
| 8 | + |
| 9 | + app = Flask(__name__) |
| 10 | + |
| 11 | + app.config["SENTRY_DSN"] = "https://[email protected]/123" |
| 12 | + flask_sentry = FlaskSentry(app) # A normal flask extension |
| 13 | + |
| 14 | + # The sentry client is set up, capture_* methods are usable anywhere |
| 15 | + capture_exception(ValueError()) |
| 16 | + |
| 17 | + @app.route("/") |
| 18 | + def index(): |
| 19 | + capture_exception(ValueError()) |
| 20 | + |
| 21 | + # logging |
| 22 | + app.logger.debug("hi") # this will be a breadcrumb |
| 23 | + app.logger.debug("oh no") # this will be an event |
| 24 | + |
| 25 | + # thrown errors are captured |
| 26 | + 1 / 0 |
| 27 | + |
| 28 | +## Configuration |
| 29 | + |
| 30 | +The Flask extension can be configured through ``app.config``. The valid keys |
| 31 | +are: |
| 32 | + |
| 33 | +* ``SENTRY_FLASK_SETUP_LOGGER``: Whether the sentry logging integration should |
| 34 | + be set up for ``app.logger``. |
| 35 | + |
| 36 | +Other options starting with ``SENTRY_`` are passed on to ``init`` (such as |
| 37 | +``dsn``) |
| 38 | + |
| 39 | +## Manual configuration |
| 40 | + |
| 41 | +If reading the configuration from ``app.config`` is too much magic for you, you |
| 42 | +can also use the lower-level API like this: |
| 43 | + |
| 44 | + from flask import Flask |
| 45 | + from sentry_sdk import init, capture_exception |
| 46 | + from sentry_sdk.integrations.flask import FlaskIntegration |
| 47 | + |
| 48 | + |
| 49 | + app = Flask(__name__) |
| 50 | + # app.config["SENTRY_DSN"] = "garbage" # would be ignored |
| 51 | + |
| 52 | + sentry_integration = FlaskIntegration(app, setup_logger=True) |
| 53 | + |
| 54 | + # client config is passed normally through init |
| 55 | + init(dsn="https://[email protected]/123", integrations=[sentry_integration]) |
| 56 | + |
| 57 | + # the routes look the same |
| 58 | + |
| 59 | + |
| 60 | +In this example, your configuration for Sentry is separate from your app |
| 61 | +config, and also the config for the Flask integration is separate from your |
| 62 | +general Sentry config. |
0 commit comments