Skip to content

Django

The logfire.instrument_django() method can be used to instrument the Django web framework with Logfire.

Installation

Install logfire with the django extra:

Terminal
pip install 'logfire[django]'

Usage

In your Django settings file, add the following lines:

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 make use of the LogfireLoggingHandler to redirect logs to Logfire.

logfire.instrument_django() uses the OpenTelemetry Django Instrumentation package, which you can find more information about here.

In case you are using Gunicorn to run your Django application, you can configure Logfire in Gunicorn as well.

Instrumenting Django ORM Queries

To instrument Django ORM queries, you need to install the associated DB instrumentation tool, then add the corresponding instrumentation command to your ‍settings file.

By default, the Django configuration uses SQLite as the database engine. To instrument it, you need to call logfire.instrument_sqlite3().

If you are using a different database, check the available instrumentation methods in our Integrations section.

Excluding URLs from instrumentation

Capturing request and response headers