# Python logging: send Django and Flask logs to Tailcove

The `tailcove` package is a standard `logging.Handler`. If your code already uses `logging`, adding Tailcove is a few lines of configuration and no changes to your log calls.

## 1. Create a source

Open **Sources** in Tailcove, click **New source** and choose **Python**. Copy the ingest token, it is shown only once, into your environment as `TAILCOVE_TOKEN`.

## 2. Install the package

```bash
pip install tailcove
```

## 3. Attach the handler

```python
import logging
import os

from tailcove import TailcoveHandler

logger = logging.getLogger("myapp")
logger.addHandler(TailcoveHandler(token=os.environ["TAILCOVE_TOKEN"]))
logger.setLevel(logging.INFO)
```

## 4. Log with structured context

Anything in `extra` lands in the line's context, keyed exactly as you wrote it:

```python
logger.info("order placed", extra={"order_id": 99, "total_cents": 4200})
```

You can then search for `order_id:99` or `total_cents>1000`.

## 5. Log exceptions properly

Use `exc_info=True` or `logger.exception()` inside an `except` block. The exception is sent as structured data (class, message, location and trace) instead of a flattened string, so repeats group into a single issue:

```python
try:
    charge_card(order)
except CardDeclined:
    logger.exception("payment failed")
```

## Secrets are masked for you

From 0.2.0 the handler masks passwords, tokens and keys before a record leaves the process, in the message, in `extra` and in exception tracebacks. `logger.info("login", extra={"password": pw})` arrives as `[redacted]`. Add your own names with `secret_keys=["customer_ssn"]`, or pass `before_send` to change or drop entries. See [Secret masking](https://tailcove.dev/docs/sending-logs/python#secret-masking).

## Django

Add the handler to your `LOGGING` setting and route `django.request` errors to it:

```python
LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "handlers": {
        "tailcove": {
            "class": "tailcove.TailcoveHandler",
            "token": os.environ["TAILCOVE_TOKEN"],
        },
    },
    "root": {"handlers": ["tailcove"], "level": "INFO"},
    "loggers": {
        "django.request": {"handlers": ["tailcove"], "level": "ERROR", "propagate": True},
    },
}
```

## Flask

```python
from flask import Flask
from tailcove import TailcoveHandler

app = Flask(__name__)
app.logger.addHandler(TailcoveHandler(token=app.config["TAILCOVE_TOKEN"]))
app.logger.setLevel("INFO")
```

## 6. Verify it works

Open the source's **Live tail** and run:

```bash
python -c "import logging, os; from tailcove import TailcoveHandler; l = logging.getLogger('t'); l.addHandler(TailcoveHandler(token=os.environ['TAILCOVE_TOKEN'])); l.warning('hello from python')"
```

The handler batches on a background thread and flushes when the interpreter exits, so the line arrives even from a one-off script.

## Next steps

- [Alert on error spikes](https://tailcove.dev/guides/alert-on-error-spikes) from your Python services.
- [Query your logs from scripts](https://tailcove.dev/guides/query-logs-with-the-api) using the read API.
- All handler options in the [Python reference](https://tailcove.dev/docs/sending-logs/python).
