Discord webhook handler for Django logging
Project description
django-discordo
A janky Discord webhook handler 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
Contrary to the name, Django is actually an optional dependency. This can also be used for logging in non-Django applications; in that case Django-specific functionality is turned off.
Configuration
1. Create a Discord Webhook
- Go to Server Settings → Integrations → Webhooks
- Click "New Webhook"
- Copy the webhook URL
2. Configure Webhook URL
There are two possible ways to do this (first one takes precedence):
Use settings.py (for Django applications)
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
}
(Webhook URL's are considered secrets,
so you probably don't want to actually hardcode the URL's into settings.py
if your source code is public.)
Using environment variables directly
If the URLs are not configured in settings.py (or Django is not present at
all) then django-discordo will check environment variables instead:
# In your .env file
DISCORD_WEBHOOK_URL=https://discord.com/api/webhooks/YOUR_WEBHOOK_URL
# Or level-specific
DISCORD_WEBHOOK_URL_ERROR=https://discord.com/api/webhooks/YOUR_ERROR_WEBHOOK_URL
3. Using the handler
In Django, add the Discord handler to your Django settings.py, e.g.
import logging
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"handlers": {
"discord": {
"class": "django_discordo.DiscordWebhookHandler",
"level": "WARNING",
},
},
"root": {
"handlers": ["discord"],
"level": "INFO",
},
}
Otherwise, import the webhook handler directly, say:
from django_discordo import DiscordWebhookHandler
logger = logging.getLogger("root")
logger.setLevel(logging.INFO)
logger.addHandler(DiscordWebhookHandler())
logger.addHandler(logging.StreamHandler())
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, "Student has submitted the problem set.")
logger.log(SUCCESS_LOG_LEVEL, "Student has found new diamond.")
logger.log(ACTION_LOG_LEVEL, "Student has updated a link in ARCH, please check it.")
Advanced Configuration
Filtering Logs
You can add filters to prevent certain logs from being sent to Discord; e.g. OTIS-WEB filters out certain 404 errors
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"],
},
},
}
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:
- The handler formats the log message with appropriate emoji and color
- Extracts metadata (user, module, filename, line number, status code)
- Includes Django request details (method, path, user agent, POST data)
- Redacts sensitive fields (passwords, tokens)
- 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_discordo-1.0.0.tar.gz.
File metadata
- Download URL: django_discordo-1.0.0.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9bfc6ac60077c285b3a16da99ccf01cb3caaa51cf7de940e3a52ab41dde3d733
|
|
| MD5 |
9aee8886cc063aae78bfea0c4d7c0d2a
|
|
| BLAKE2b-256 |
04eaeec694d2bfb19a34b5b16cc90a8b89a0a11d9f3df83bc1e5e1b00878963e
|
Provenance
The following attestation bundles were made for django_discordo-1.0.0.tar.gz:
Publisher:
release.yml on vEnhance/django-discordo
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_discordo-1.0.0.tar.gz -
Subject digest:
9bfc6ac60077c285b3a16da99ccf01cb3caaa51cf7de940e3a52ab41dde3d733 - Sigstore transparency entry: 774609389
- Sigstore integration time:
-
Permalink:
vEnhance/django-discordo@0eea9cc2beb7ece2aadb320a6368ce9eed24f557 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/vEnhance
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@0eea9cc2beb7ece2aadb320a6368ce9eed24f557 -
Trigger Event:
release
-
Statement type:
File details
Details for the file django_discordo-1.0.0-py3-none-any.whl.
File metadata
- Download URL: django_discordo-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9864e6cc2ca8233ba15673d7f4eb7e39d2993c4ae623457dec595d440f0fdf7b
|
|
| MD5 |
faf1676029a4c8371b9273603ac185e1
|
|
| BLAKE2b-256 |
44b06c715825f516ebb302969977a41a6cc94d07d780afc5ec68b0bed0326e62
|
Provenance
The following attestation bundles were made for django_discordo-1.0.0-py3-none-any.whl:
Publisher:
release.yml on vEnhance/django-discordo
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_discordo-1.0.0-py3-none-any.whl -
Subject digest:
9864e6cc2ca8233ba15673d7f4eb7e39d2993c4ae623457dec595d440f0fdf7b - Sigstore transparency entry: 774609391
- Sigstore integration time:
-
Permalink:
vEnhance/django-discordo@0eea9cc2beb7ece2aadb320a6368ce9eed24f557 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/vEnhance
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@0eea9cc2beb7ece2aadb320a6368ce9eed24f557 -
Trigger Event:
release
-
Statement type: