Flask
=====
Installation
------------
If you haven't already, install raven with its explicit Flask dependencies::
pip install raven[flask]
Setup
-----
The first thing you'll need to do is to initialize Raven under your application::
from raven.contrib.flask import Sentry
sentry = Sentry(app, dsn='___DSN___')
If you don't specify the ``dsn`` value, we will attempt to read it from
your environment under the ``SENTRY_DSN`` key.
Extended Setup
--------------
You can optionally configure logging too:
.. sourcecode:: python
import logging
from raven.contrib.flask import Sentry
sentry = Sentry(app, logging=True, level=logging.ERROR, \
logging_exclusions=("logger1", "logger2", ...))
Building applications on the fly? You can use Raven's ``init_app`` hook:
.. sourcecode:: python
sentry = Sentry(dsn='http://public_key:secret_key@example.com/1')
def create_app():
app = Flask(__name__)
sentry.init_app(app)
return app
You can pass parameters in the ``init_app`` hook:
.. sourcecode:: python
sentry = Sentry()
def create_app():
app = Flask(__name__)
sentry.init_app(app, dsn='___DSN___', logging=True,
level=logging.ERROR,
logging_exclusions=("logger1", "logger2", ...))
return app
Settings
--------
Additional settings for the client can be configured using
``SENTRY_CONFIG`` in your application's configuration:
.. sourcecode:: python
class MyConfig(object):
SENTRY_CONFIG = {
'dsn': '___DSN___',
'include_paths': ['myproject'],
'release': raven.fetch_git_sha(os.path.dirname(__file__)),
}
If `Flask-Login
The error identifier is {{ g.sentry_event_id }}
{% endif %} .. _python-flask-user-feedback: User Feedback ------------- To enable user feedback for crash reports just make sure you have a custom `500` error handler and render out a HTML snippet for bringing up the crash dialog: .. sourcecode:: python from flask import Flask, g, render_template from raven.contrib.flask import Sentry app = Flask(__name__) sentry = Sentry(app, dsn='___DSN___') @app.errorhandler(500) def internal_server_error(error): return render_template('500.html', event_id=g.sentry_event_id, public_dsn=sentry.client.get_public_dsn('https') ) And in the error template (``500.html``) you can then do this: .. sourcecode:: html+jinja {% if event_id %} {% endif %} That's it! For more details on this feature, see the :doc:`User Feedback guide <../../../learn/user-feedback>`. Dealing With Proxies -------------------- When your Flask application is behind a proxy such as nginx, Sentry will use the remote address from the proxy, rather than from the actual requesting computer. By using ``ProxyFix`` from `werkzeug.contrib.fixers