Skip to main content

A logging handler that sends log messages to discord via webhook.

Project description

Discord Logging Handler

PyPI Downloads PyPI version Python Version License

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 to ERROR.
  • dev_mode (optional): Use to enable development mode (logs are suppressed during builds/tests). Defaults to False.

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: Discord output

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.

  1. Go to Server Settings → Integrations
  2. Under Webhooks, click New Webhook
  3. Copy the generated webhook URL

server settings webhooks

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

discord_logging_handler-0.3.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

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

discord_logging_handler-0.3.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

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

Hashes for discord_logging_handler-0.3.0.tar.gz
Algorithm Hash digest
SHA256 4b7597b2600e36183bcc2b11cb697d1fa48858d95e71ebb3d1b8eb87fd0caed7
MD5 4273a98646137875017bf148a9411afd
BLAKE2b-256 b5d7690e84ccb9c27b4d6e89cca4a46e4a531b9df78671870cf155b25dc2d87d

See more details on using hashes here.

File details

Details for the file discord_logging_handler-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for discord_logging_handler-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f9d44ba828b70066cb83501ded4f352e59879b168b874b9ec8f81794eaa559c0
MD5 f54cfe8279a028b8649795f5f6af6625
BLAKE2b-256 36f619bf21879390622e006694cc9ac89b33cbdd4a2040c136476dbc36b37eb1

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