Skip to content

Commit 8e41a06

Browse files
committed
chore: documentation for flask
1 parent 11a4357 commit 8e41a06

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

docs/flask.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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.

tests/integrations/flask/test_flask.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,16 @@ def index():
267267
client.get("/")
268268

269269
assert len(capture_events) == 1
270+
271+
272+
def test_logging(capture_events, app):
273+
@app.route("/")
274+
def index():
275+
app.logger.error("hi")
276+
return "ok"
277+
278+
client = app.test_client()
279+
client.get("/")
280+
281+
event, = capture_events
282+
assert event["level"] == "error"

0 commit comments

Comments
 (0)