Skip to content

regulad/dislog

Repository files navigation

wakatime GitHub Workflow Status pre-commit.ci status PyPI PyPI - Python Version

Provides an interface for using a Discord webhook as a logger.

Designed to abstract away webhook-specific details, such as the JSON format, and provide a simple interface for logging messages.

Install

Simple as:

pip install dislog[discordpy]

You can use extras to define which implementation of discord.py you want to use.

  • discordpy: `discord.py

Example

Using dislog in your projects is dead simple. It behaves like any other logging.Handler.

For performance reasons, it even fires off a new thread for each log message, so you don't have to worry about blocking your main thread with costly HTTP requests.

from dislog import DiscordWebhookHandler
from logging import *

basicConfig(level=ERROR, handlers=[StreamHandler(), DiscordWebhookHandler("url", text_send_on_error="<@440468612365680650>")])

error("hi")

Demo

It also works with asynchronous code, simply pass the run_async keyword argument. This is optional and makes it use the event loop instead of a thread pool.

from dislog import DiscordWebhookHandler
from logging import *
from asyncio import run, sleep, get_running_loop

async def main():
    basicConfig(level=ERROR, handlers=[StreamHandler(), DiscordWebhookHandler("url", event_loop=get_running_loop(), text_send_on_error="<@440468612365680650>")])

    error("hi")

    await sleep(1)  # Give it some time to run!

run(main())

Async Demo

Contributing

Setup

git clone https://github.com/regulad/dislog
cd dislog
pip install -e .
pre-commit install

Testing

tox