Python logging: send Django and Flask logs to Tailcove
Make Python logging searchable. Ship logs and exceptions from the standard logging module, Django and Flask to Tailcove in ten minutes.
Before you start
- A Python 3 application
- A Tailcove account with a source
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.
On this page10 sections
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#
pip install tailcove
3. Attach the handler#
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:
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:
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.
Django#
Add the handler to your LOGGING setting and route django.request errors to it:
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#
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:
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 from your Python services.
- Query your logs from scripts using the read API.
- All handler options in the Python reference.
Ready to try it on your own logs?
The Free plan includes a source, live tail, search and alerts. No credit card.