Skip to main content

A small discord handler for the logging module using webtokens

Project description

DiscordHandler

This class is using the integrated logging class from Python and requests from PyPi.

Feel free to change and add some good stuff.

Installing

Install and update using pip:

pip install discord-handler

Requirements

Installing requests:

pip install requests

Get Started:

First you need Discord or some Channels, where you have access to create a new Webhook.

You find Webhooks options in the channel settings. Create a new Webhook and copy the Webhook Url. Note: Do NOT give this URL out to the public like me in previous commits. (I changed it :P )

You can test the logging class by using the Test file.

If u want to know, how to send messages via POST and requests to Discord you can see an example in Discord file.

Attention:

Discord only allows 2 000 characters, so you might get trouble when using large messages. To limit the length of the message use max_size in the constructor. See Example below. Thanks to racinmat for the implementation.

Mentions:

To use mentions use the paramter notify_users. You can use everyone without id and private mentions with id. To get your user id you need to activate the developer mode and right click on your name.

Example:

    from discord_handler import DiscordHandler

    webhook_url = "Your Webhook here"
    agent = "My Application"

    logger = logging.getLogger("My Application")
    logger.setLevel(logging.DEBUG)

    # Create formatter
    FORMAT = logging.Formatter(
        "%(asctime)s - %(name)s - %(levelname)s - %(message)s")

    # Create DiscordHandler and StreamHandler
    # This will mention everyone. You can use your id here for private mentions.
    # Emits log as raw text, so strings can contain Discord markdown formatting such as __underline__ or **bold**
    notify_users = ['everyone']
    discord_handler = DiscordHandler(webhook_url, agent, notify_users=notify_users, emit_as_code_block=False, max_size=20000)
    stream_handler = logging.StreamHandler()

    # Add log level to handlers
    discord_handler.setLevel(logging.WARNING)
    stream_handler.setLevel(logging.DEBUG)

    # Add format to handlers
    discord_handler.setFormatter(FORMAT)
    stream_handler.setFormatter(FORMAT)

    # Add the handlers to the Logger
    logger.addHandler(discord_handler)
    logger.addHandler(stream_handler)

    logger.debug("Logger created")

For more Infos: Logging Cookbook

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_handler-0.0.5.tar.gz (4.1 kB view details)

Uploaded Source

File details

Details for the file discord_handler-0.0.5.tar.gz.

File metadata

  • Download URL: discord_handler-0.0.5.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.4

File hashes

Hashes for discord_handler-0.0.5.tar.gz
Algorithm Hash digest
SHA256 3691509edb37eb77773c578f8cdba4a8654e77dbf6cffc7f9f558baa9e014d5d
MD5 f41f944e2a359c383a56ac08a0c62133
BLAKE2b-256 837648edb67b9e10a917fa48e13ee6889bbad8572fd76601aee2412e04967fcb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page