Skip to content

Django

See every request your Django app handles (the view it hit, the URL, how long it took, the database queries it ran, and any errors) as a trace (the full journey of one request, made of nested spans, where each span is one unit of work with a name, a start, and a duration) in Logfire.

What you’ll capture

  • Each request as a span, with its HTTP status and duration
  • The matched view and URL route
  • The database queries run during the request (once you instrument your database engine, see below)
  • Any errors raised while handling the request

Before you start

You’ll need a Logfire project. Open Add data in your project (top navigation) and follow the setup for your language: it signs your machine in with logfire auth (a browser sign-in, no token to copy) and, for production or other languages, creates a write token (the credential your app uses to send data). New to Logfire? Start with Getting Started.

Installation

Install logfire with the django extra:

Terminal
pip install 'logfire[django]'

Usage

Add two lines to your Django settings file: logfire.configure() to connect to your project, and logfire.instrument_django() to record every request. The same example also routes your standard-library log messages to Logfire.

import logfire

# ...All the other settings...

LOGGING = {  # (1)
  'version': 1,
  'disable_existing_loggers': False,
  'handlers': {
      'logfire': {
          'class': 'logfire.LogfireLoggingHandler',
      },
  },
  'root': {
      'handlers': ['logfire'],
  },
}

# Add the following lines at the end of the file
logfire.configure()
logfire.instrument_django()

Django uses the standard library logging module, and can be configured using the dictConfig format. As per our dedicated logging section, you can use the LogfireLoggingHandler to send your log messages to Logfire.

Verify it worked

Start your app (for example, python manage.py runserver) and open one of your pages in the browser.

Then open your project in the Logfire web app and go to the Live view. Within a few seconds you should see a span for the request. Click it to see its duration, the view that handled it, and the response status.

Troubleshooting

Not seeing your requests in Logfire? Check these first:

  • logfire.configure() runs before logfire.instrument_django(). Configure the connection first, then instrument.
  • You call instrument_django() exactly once, at the very end of your settings file so nothing else runs after it.
  • Your write token is set. In local development, run logfire projects use <your-project>; in production, set the LOGFIRE_TOKEN environment variable. See Getting Started.
  • You actually sent a request. Spans appear only after a view is hit; reload a page.

Advanced

Instrumenting database queries

To see the database queries run during each request, instrument your database engine as well.

By default, Django uses SQLite as the database engine. To instrument it, call logfire.instrument_sqlite3(). If you use a different database, find the matching instrumentation method in our Integrations section.

Running under Gunicorn

If you run your Django application with Gunicorn, you can also configure Logfire in Gunicorn.

Reference

Excluding URLs from instrumentation

Capturing request and response headers