Skip to main content

The missing debug tool for Django, inspired by Telescope.

Project description

DjangoSonar

The missing debug tool for django.

DjangoSonar is a comprehensive debugging and introspection tool for Django applications, inspired by Laravel Telescope.

image

🥳 Motivation

Having spent years developing with Laravel before switching to Django, the first thing I missed from this change was the amazing Laravel Telescope. So, I decided to create it myself.

DjangoSonar is built using:

If you use this project, please consider giving it a ⭐.

⭐ Features

  • Self updating lists of:
    • Requests
    • Exceptions
    • Queries
    • Dumps
    • Events
    • Logs
    • (Signals coming soon™)
  • Request insights:
    • Payload get/post
    • Auth User
    • Session vars
    • Headers
    • ...
  • 🔒 Automatic sensitive data filtering (passwords, tokens, API keys, etc.)
  • Historical data (clearable)
  • Simple and reactive UI

🛠️ How to install

  1. First you need to install the package:
pip install django-sonar
  1. Then, to enable the dashboard, you will need to add the app to the INSTALLED_APPS in your project main settings file:
INSTALLED_APPS = [
    ...
    'django_sonar',
    ...
]
  1. Add the urls to the main urls.py file in your project folder:
urlpatterns = [
    ...
    path('sonar/', include('django_sonar.urls')),
    ...
]
  1. 🔔 Be sure to add the exclusions settings too, or you will get way too much data in your sonar dashboard:
DJANGO_SONAR = {
    'excludes': [
        STATIC_URL,
        MEDIA_URL,
        '/sonar/',
        '/admin/',
        '/__reload__/',
    ],
}

In this example I'm excluding all the http requests to static files, uploads, the sonar dashboard itself, the django admin panels and the browser reload library. Update this setting accordingly, YMMW.

🔒 Sensitive Data Protection

DjangoSonar automatically filters sensitive data from request payloads, headers, and session data before storing them in the database. By default, it masks common sensitive fields like:

  • password, passwd, pwd, pass
  • token, api_key, secret, authorization
  • credit_card, cvv, ssn, pin
  • And more...

These fields are replaced with ***FILTERED*** in the stored data.

Custom Sensitive Fields: You can add your own sensitive field patterns by adding them to the configuration:

DJANGO_SONAR = {
    'excludes': [
        STATIC_URL,
        MEDIA_URL,
        '/sonar/',
    ],
    'sensitive_fields': [
        'custom_secret',
        'internal_api_key',
        'private_data',
    ],
}

The filtering is case-insensitive and works with partial matches (e.g., user_password, my_api_key will also be filtered).

💾 Separate Database Configuration (Recommended)

For better performance, it's recommended to store Django Sonar data in a separate database. This prevents monitoring overhead from impacting your main application database.

Step 1: Configure a separate database in your settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'your_main_db',
        # ... other settings
    },
    'sonar_db': {
        'ENGINE': 'django.db.backends.sqlite3',  # or postgresql, mysql, etc.
        'NAME': BASE_DIR / 'sonar.db',
    }
}

Step 2: Add the database router to your settings.py:

DATABASE_ROUTERS = ['django_sonar.db_router.SonarDatabaseRouter']

Step 3: Run migrations:

# Migrate your main database (Django Sonar tables will be skipped)
python manage.py migrate

# Migrate the sonar database (only Django Sonar tables)
python manage.py migrate --database=sonar_db

Now all Django Sonar data will be stored in the separate database!

Note: The database router ensures that:

  • Django Sonar tables only migrate to sonar_db
  • Your main application tables never migrate to sonar_db
  • Everything stays cleanly separated
  1. Now you should be able to execute the migrations to create the two tables that DjangoSonar will use to collect the data.
python manage.py migrate
  1. And finally add the DjangoSonar middleware to your middlewares to enable the data collection:
MIDDLEWARE = [
  ...
  'django_sonar.middlewares.requests.RequestsMiddleware',
  ...
]

😎 How to use

The Dashboard

To access the dashboard you will point your browser to the /sonar/ url (but you can change it as described before). The interface is very simple and self explanatory.

You could use DjangoSonar in production too, since it gives you an historical overview of all the requests, but be sure to clear the data and disable it when you have debugged the problem.

🔔 If you forget to disable/clear DjangoSonar you could end up with several gigabytes of data collected. So please use it with caution when in production 🔔

Only authenticated superusers can access sonar. If you are trying to access the dashboard with a wrong type of user, you will see an error page, otherwise you should see the DjangoSonar login page.

sonar() - the dump helper

You can dump values to DjangoSonar using the sonar() helper function:

from django_sonar.utils import sonar

sonar('something')

And you can also dump multiple values like this:

from django_sonar.utils import sonar

sonar('something', self.request.GET, [1,2,3])

Tracking events with sonar_event()

Use sonar_event() to push structured domain events into Sonar during a request:

from django_sonar import sonar_event

sonar_event(
    'billing.invoice_paid',
    payload={'invoice_id': 123, 'amount': 49.90, 'currency': 'USD'},
    level='info',
    tags=['billing', 'payments'],
)

Tracked fields:

  • name
  • level
  • payload (shown in the Event Data column)
  • tags
  • timestamp

These entries are shown in the Events panel.

Tracking logs with SonarHandler

Attach django_sonar.logging.SonarHandler to Django logging to track log entries in Sonar:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {'class': 'logging.StreamHandler'},
        'sonar': {'class': 'django_sonar.logging.SonarHandler'},
    },
    'loggers': {
        '': {
            'handlers': ['console', 'sonar'],
            'level': 'INFO',
        },
    },
}

Example usage:

import logging

logger = logging.getLogger(__name__)

logger.warning(
    'Payment retry scheduled',
    extra={
        'context': {'invoice_id': 123, 'attempt': 2},
        'queue': 'payments',
    },
)

Tracked fields:

  • logger
  • level
  • message
  • context (all extra fields are normalized into this)
  • timestamp

These entries are shown in the Logs panel.

Note: events/logs are buffered per request and persisted by the Sonar middleware at request end.

🧩 Custom Panels (For Developers)

DjangoSonar supports extension panels that are auto-registered from settings, so developers can add new dashboard sections without forking core views/templates.

1. Create a panel class

Create a module in your app, for example myapp/sonar_panels.py:

from django_sonar.panels import SonarPanel


class EventsPanel(SonarPanel):
    key = 'events'
    label = 'Events'
    icon = 'bi-calendar-event'
    category = 'events'
    list_template = 'myapp/sonar/events_list.html'
    # optional:
    # detail_template = 'myapp/sonar/events_detail.html'
    # list_context_name = 'events'

Panel keys must be unique. Key collisions with built-ins or other custom panels raise an explicit configuration error.

2. Register your panel in settings

DJANGO_SONAR = {
    'excludes': [
        STATIC_URL,
        MEDIA_URL,
        '/sonar/',
        '/admin/',
    ],
    'custom_panels': [
        'myapp.sonar_panels.EventsPanel',
    ],
}

3. Add the panel template

Create the template referenced by list_template, for example myapp/templates/myapp/sonar/events_list.html.

DjangoSonar renders this template at:

  • /sonar/p/events/ (list)
  • /sonar/p/events/<uuid>/ (detail, only if detail_template is defined)

The panel is also automatically shown in the Sonar sidebar navigation.

4. Store data under your panel category

Panel list queries use SonarData.category by default.
For the example above, save entries with category events (for example via middleware/hooks/services) and they will appear in your custom panel.

⚖️ License

DjangoSonar is open-sourced software licensed under the MIT license.

🍺 Donations

If you really like this project and you want to help me please consider buying me a beer 🍺

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_sonar-0.5.1.tar.gz (548.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_sonar-0.5.1-py3-none-any.whl (560.8 kB view details)

Uploaded Python 3

File details

Details for the file django_sonar-0.5.1.tar.gz.

File metadata

  • Download URL: django_sonar-0.5.1.tar.gz
  • Upload date:
  • Size: 548.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for django_sonar-0.5.1.tar.gz
Algorithm Hash digest
SHA256 c460e1c2d9a73d5bc6e7ef9ab9fa64e62637b1c773bf6c99817b296d752d1fd6
MD5 fdea1a8238a1022af37a571cfa5b3980
BLAKE2b-256 b25d45248cfccbaaef12b5efe4bc024ef27cb8c335a937aab36f5bd449a329a8

See more details on using hashes here.

File details

Details for the file django_sonar-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: django_sonar-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 560.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for django_sonar-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 012d51fe8ce04ff6570b66df803656c8e3477afbe20943ae66e1314fd81b3d56
MD5 950ce6cdce85568559512e7bae5fb611
BLAKE2b-256 a8cd336e03606b5c6501b871e5e98694f1ad5d11b324aa869901cfbd5ef67b12

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page