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

Changelog

See the full Changelog for details on version history.

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.1.tar.gz (3.7 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.1-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file discord_logging_handler-0.3.1.tar.gz.

File metadata

  • Download URL: discord_logging_handler-0.3.1.tar.gz
  • Upload date:
  • Size: 3.7 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.1.tar.gz
Algorithm Hash digest
SHA256 950d8663f1ffd080f96f4e183394277eb6a3a0e494834d8d6fccacadba649abd
MD5 787e4411a28b7c85724e54138051ebf5
BLAKE2b-256 8d91e63434c5554844ea7f57ff49a5f22203bf60c377e170cf4f0f0f8f7eb445

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for discord_logging_handler-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ee3efa1653dcabc77ca2bc102155e03966c6b8a3eb7f4ce70876a52f1b780f3c
MD5 d4b1615b20e565d5b41058865c5208dc
BLAKE2b-256 4383ae0ef4e977e205452b7b726621218671ded0ad6ff6bfbc0c4197a53b906e

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