Skip to main content

Discord webhook handler for Django logging

Project description

django-discordo

A janky Discord webhook handler for Django logging originally written for OTIS-WEB.

Features, apparently

  • 🎨 Color-coded log levels with emoji indicators
  • 📝 Automatic formatting of Django request data
  • 🔒 Automatic redaction of sensitive data (passwords, tokens)
  • 🎯 Support for custom log levels (VERBOSE, SUCCESS, ACTION)
  • 🔧 Configurable webhook URLs per log level
  • 📊 Rich metadata including user info, status codes, and stack traces

Installation

For example, if using uv:

uv add django-discordo

Configuration

1. Create a Discord Webhook

  1. Go to Server Settings → Integrations → Webhooks
  2. Click "New Webhook"
  3. Copy the webhook URL

2. Configure Webhook URL

Add your webhook URL to your Django settings.py:

# Simple configuration - single webhook for all log levels
DISCORD_WEBHOOK_URL = "https://discord.com/api/webhooks/YOUR_WEBHOOK_URL"

# OR: Advanced configuration - different webhooks per log level
DISCORD_WEBHOOK_URLS = {
    "CRITICAL": "https://discord.com/api/webhooks/CRITICAL_WEBHOOK",
    "ERROR": "https://discord.com/api/webhooks/ERROR_WEBHOOK",
    "WARNING": "https://discord.com/api/webhooks/WARNING_WEBHOOK",
    "DEFAULT": "https://discord.com/api/webhooks/DEFAULT_WEBHOOK",  # Fallback for other levels
}

Alternative: You can also use environment variables (the handler will check settings first, then fall back to environment variables):

# In your .env file
WEBHOOK_URL=https://discord.com/api/webhooks/YOUR_WEBHOOK_URL
# Or level-specific
WEBHOOK_URL_ERROR=https://discord.com/api/webhooks/YOUR_ERROR_WEBHOOK_URL

3. Update Django Settings

Add the Discord handler to your Django settings.py:

import logging
from django_discordo import ACTION_LOG_LEVEL, SUCCESS_LOG_LEVEL, VERBOSE_LOG_LEVEL

# Custom log levels are automatically registered when you import django_discordo

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "handlers": {
        "discord": {
            "class": "django_discordo.DiscordWebhookHandler",
            "level": "WARNING",  # Adjust as needed
        },
    },
    "root": {
        "handlers": ["discord"],
        "level": "INFO",
    },
}

Custom Log Levels

django-discordo provides three custom log levels in addition to Django's standard levels:

  • VERBOSE (level 15): Between DEBUG and INFO, for detailed but not critical information
  • SUCCESS (level 25): Between INFO and WARNING, for successful operations
  • ACTION (level 35): Between WARNING and ERROR, for important user actions

Usage Example

import logging
from django_discordo import VERBOSE_LOG_LEVEL, SUCCESS_LOG_LEVEL, ACTION_LOG_LEVEL

logger = logging.getLogger(__name__)

# Using custom log levels
logger.log(VERBOSE_LOG_LEVEL, "Detailed operation info")
logger.log(SUCCESS_LOG_LEVEL, "User registration completed successfully")
logger.log(ACTION_LOG_LEVEL, "Admin user modified critical settings")

Advanced Configuration

Filtering Logs

You can add filters to prevent certain logs from being sent to Discord:

def filter_useless_404(record):
    if record.args and len(record.args) >= 2:
        return "wp-include" not in str(record.args[1])
    return True

LOGGING = {
    "filters": {
        "filter_useless_404": {
            "()": "django.utils.log.CallbackFilter",
            "callback": filter_useless_404,
        },
    },
    "handlers": {
        "discord": {
            "class": "django_discordo.DiscordWebhookHandler",
            "level": "WARNING",
            "filters": ["filter_useless_404"],
        },
    },
}

Level-Specific Webhooks

You can route different log levels to different Discord channels by setting level-specific webhook URLs:

WEBHOOK_URL_CRITICAL=https://discord.com/api/webhooks/CRITICAL_CHANNEL
WEBHOOK_URL_ERROR=https://discord.com/api/webhooks/ERROR_CHANNEL
WEBHOOK_URL_WARNING=https://discord.com/api/webhooks/WARNING_CHANNEL
WEBHOOK_URL=https://discord.com/api/webhooks/DEFAULT_CHANNEL

The handler will check for WEBHOOK_URL_{LEVELNAME} first, then fall back to WEBHOOK_URL.

Testing Mode

When running tests, you may want to disable Discord logging to avoid spam:

import logging
from django_discordo import ACTION_LOG_LEVEL

if TESTING:
    logging.disable(ACTION_LOG_LEVEL)

This disables all logs at ACTION level and below (including SUCCESS, INFO, VERBOSE, and DEBUG).

How It Works

When a log record is emitted:

  1. The handler formats the log message with appropriate emoji and color
  2. Extracts metadata (user, module, filename, line number, status code)
  3. Includes Django request details (method, path, user agent, POST data)
  4. Redacts sensitive fields (passwords, tokens)
  5. Sends a beautifully formatted embed to Discord via webhook

Discord Embed Format

Each log message appears as a Discord embed with:

  • Title: Log message (with emoji indicator)
  • Color: Coded by log level (red for errors, yellow for warnings, etc.)
  • Fields: Status code, log level, module, user, filename
  • Description: Detailed message, exception traceback, and request data

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_discordo-0.1.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

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

django_discordo-0.1.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file django_discordo-0.1.0.tar.gz.

File metadata

  • Download URL: django_discordo-0.1.0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_discordo-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d84546c6d76952ecb65e7f96b42fe87c7462be49961803171148305a6f7265e2
MD5 ef7891f81d41c0dbf72e536173f7e00a
BLAKE2b-256 a40d29c713f70089661220a2c6a8fbc38b2891b6fb706ff55cd68ce6c42c0132

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_discordo-0.1.0.tar.gz:

Publisher: release.yml on vEnhance/django-discordo

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_discordo-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_discordo-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2d003bc36c180528aca3ae48c6f2eca0b56fbf3698ef3015f2bfe57d61abba5b
MD5 057a21263a98dba91fc12b5711e90f84
BLAKE2b-256 4746216756263d2cea94f05a28103eb87885993f1f05b4cdc8fbca75e4aa4eb3

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_discordo-0.1.0-py3-none-any.whl:

Publisher: release.yml on vEnhance/django-discordo

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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