# 13.1.sentry实现错误日志监控

文档请参考：<https://docs.sentry.io/platforms/python/guides/django/>

## Install

Install our Python SDK using [`pip`](https://pip.pypa.io/en/stable/):

```bash
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

```python
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](https://docs.sentry.io/platforms/python/guides/django/configuration/integrations/django/).

## Verify

This snippet includes an intentional error, so you can test that everything is working as soon as you set it up:

```python
from django.urls import path

def trigger_error(request):
    division_by_zero = 1 / 0

urlpatterns = [
    path('sentry-debug/', trigger_error),
    # ...
]
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://moluo.gitbook.io/notes/bian-cheng-xue-xi/python/django-rest-framework/13.1.sentry-shi-xian-cuo-wu-ri-zhi-jian-kong.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
