13.1.sentry实现错误日志监控
文档请参考:https://docs.sentry.io/platforms/python/guides/django/
Install
Install our Python SDK using pip
:
pip install --upgrade sentry-sdk
Configure
Configuration should happen as early as possible in your application's lifecycle.
Initialize the Python SDK with the Django integration in your settings.py
file. Once this is done, the SDK captures all unhandled exceptions and transactions.
settings.py
import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration
sentry_sdk.init(
dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
integrations=[DjangoIntegration()],
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for performance monitoring.
# We recommend adjusting this value in production,
traces_sample_rate=1.0,
# If you wish to associate users to errors (assuming you are using
# django.contrib.auth) you may enable sending PII data.
send_default_pii=True
)
Additional configuration for DjangoIntegration
can be found under integration configuration.
Verify
This snippet includes an intentional error, so you can test that everything is working as soon as you set it up:
from django.urls import path
def trigger_error(request):
division_by_zero = 1 / 0
urlpatterns = [
path('sentry-debug/', trigger_error),
# ...
]
Last updated
Was this helpful?