A logging handler that sends log messages to discord via webhook.
Project description
Discord Logging Handler
A Python logging handler that sends log messages to Discord via webhook with colour-coded levels.
Features
- Colour-coded embeds by log level
- Supports full stack traces for errors and exceptions
- Configurable minimum log level
- Simple drop-in integration for any Python project
- Optional development mode to suppress logs during builds/tests
Installation
pip install discord-logging-handler
Usage
from discord_logging_handler import DiscordWebHookHandler
import logging
handler = DiscordWebHookHandler(webhook_url="WEBHOOK_URL", level=logging.INFO, dev_mode=True)
webhook_url: Your webhook from Discord Integrations.level(optional): The minimum level that should trigger logs to be sent to Discord. Defaults toERROR.dev_mode(optional): Use to enable development mode (logs are suppressed during builds/tests). Defaults toFalse.
Example
from discord_logging_handler import DiscordWebHookHandler
import logging
# Create handler
handler = DiscordWebHookHandler(webhook_url="WEBHOOK_URL", level=logging.INFO, dev_mode=False)
# Set up the root logger
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.addHandler(handler)
# Example log messages
logger.info("Server started successfully.")
logger.warning("Disk space running low.")
logger.error("Database connection failed.")
# Example exception handling
try:
1 / 0
except Exception:
logger.exception("An unexpected error occurred.")
Example Output
Logs appear directly in your Discord channel with level-based colour coding:
Django Example
settings.py
import os
from pathlib import Path
from discord_logging_handler import DiscordWebHookHandler # ensure it's installed
BASE_DIR = Path(__file__).resolve().parent.parent
DISCORD_WEBHOOK_URL = os.environ.get('DISCORD_WEBHOOK_URL')
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'file': {
'level': 'INFO',
'class': 'logging.FileHandler',
'filename': BASE_DIR / 'logs/app.log',
'formatter': 'verbose',
},
'console': {
'class': 'logging.StreamHandler',
'formatter': 'simple'
},
'discord': {
'level': 'INFO',
'class': 'discord_logging_handler.handler.DiscordWebHookHandler',
'webhook_url': DISCORD_WEBHOOK_URL,
'dev_mode': False
}
},
'root': {
'handlers': ['console', 'file', 'discord'],
'level': 'INFO',
},
'loggers': {
'django': {
'handlers': ['console', 'file'],
'level': 'CRITICAL',
'propagate': True,
},
'vaultapi': {
'handlers': ['console', 'file', 'discord'],
'level': 'INFO',
'propagate': False,
},
},
}
Environment Variable
DISCORD_WEBHOOK_URL: Your Discord webhook URL.
Discord Webhook Setup
Discord allows webhooks to be created via Server Settings → Integrations.
- Go to Server Settings → Integrations
- Under Webhooks, click New Webhook
- Copy the generated webhook URL
Contributing
Contributions, issues, and feature requests are welcome!
Feel free to open an issue or submit a pull request on GitHub.
License
MIT License
Copyright (c) 2025 Moses White
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 discord_logging_handler-0.3.0.tar.gz.
File metadata
- Download URL: discord_logging_handler-0.3.0.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4b7597b2600e36183bcc2b11cb697d1fa48858d95e71ebb3d1b8eb87fd0caed7
|
|
| MD5 |
4273a98646137875017bf148a9411afd
|
|
| BLAKE2b-256 |
b5d7690e84ccb9c27b4d6e89cca4a46e4a531b9df78671870cf155b25dc2d87d
|
File details
Details for the file discord_logging_handler-0.3.0-py3-none-any.whl.
File metadata
- Download URL: discord_logging_handler-0.3.0-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f9d44ba828b70066cb83501ded4f352e59879b168b874b9ec8f81794eaa559c0
|
|
| MD5 |
f54cfe8279a028b8649795f5f6af6625
|
|
| BLAKE2b-256 |
36f619bf21879390622e006694cc9ac89b33cbdd4a2040c136476dbc36b37eb1
|