Skip to main content

A Python logging handler which sends its logs to a Discord Channel

Project description

discord-lumberjack

A Python logging handler which sends its logs to a Discord Channel or Webhook.

Documentation

You can find the documentation here.

Installation

To install this python module, run the following command

$ pip install discord-lumberjack

Handlers

This python module provides several logging handlers (located in the discord_lumberjack.handlers module) which will send the logs it recieves to a Discord webhook, server channel, or DM channel.

The available handlers are:

  • DiscordChannelHandler - Uses a bot token and a channel ID to send logs to the given channel from the given bot.
  • DiscordDMHandler - Uses a bot token and a user ID to send logs to the given user from the given bot.
  • DiscordWebhookHandler - Uses a webhook URL to send the logs to.
  • DiscordHandler - This is the base class for the other three. You probably don't want to use this unless you're creating your own fancy handler.

Message Creators

In order to send nice looking messages, there are a few message creators available (located in the discord_lumberjack.message_creators module). These are responsible for converting a logging.LogRecord into a message structure that will be sent to Discord's API.

The message creators provided currently will split extremely long messages into several in order to fit within Discord's message limits. If you decide to create your own one, keep that in mind too.

The available message creators are:

  • BasicMessageCreator - This is a simple message creator which will use the handler's set formatter to send the message as plain text. By default, the message will be formatted in monospace, but this can be disabled via the constructor.
  • EmbedMessageCreator - This message creator will create a fancy-looking embed message from the log record. It will ignore the handler's formatter.

Usage

The easiest way to get started is to create a webhook and use that, but if you're using this to log a Discord bot, you can use it's token directly, without needing to create webhooks.

Import

First, you should import the handlers you want to use. For this example, we'll assume we have a Discord bot and we'd like to use it to log every message to a channel and also to send errors to a DM.

We'll be using the DiscordChannelHandler to send all messages of level INFO and above to the channel and DiscordDMHandler to send messages of level ERROR and above to a DM.

from discord_lumberjack.handlers import DiscordChannelHandler, DiscordDMHandler

Basic Setup

You should really read the documentation for the logging module to learn how to set up your logging, but here's a quick snippet to get you started.

import logging
logging.basicConfig(
	level=logging.INFO,
	handlers=[
		DiscordChannelHandler(token=my_bot_token, channel_id=my_channel_id),
		DiscordDMHandler(token=my_bot_token, user_id=my_user_id, level=logging.ERROR),
	]
)

Once you've set up your logging, you can start logging messages like this:

logging.info("This is an informative message that will be sent to the channel.")
logging.error("This is an error, so it will also be sent to the DM.")

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-lumberjack-1.0.4.tar.gz (25.5 kB view details)

Uploaded Source

Built Distribution

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

discord_lumberjack-1.0.4-py3-none-any.whl (29.0 kB view details)

Uploaded Python 3

File details

Details for the file discord-lumberjack-1.0.4.tar.gz.

File metadata

  • Download URL: discord-lumberjack-1.0.4.tar.gz
  • Upload date:
  • Size: 25.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

File hashes

Hashes for discord-lumberjack-1.0.4.tar.gz
Algorithm Hash digest
SHA256 6f066c2b986a1f8ab75c19db838068d5639df707576025fa7e145c2f10d30317
MD5 f151cb3c1ba57e6308970ceb5f6b9f0f
BLAKE2b-256 b374d8319a19b9ac33211cf96f7b4d830169c7ba100e1a6840d0f245d8aff812

See more details on using hashes here.

File details

Details for the file discord_lumberjack-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: discord_lumberjack-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 29.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

File hashes

Hashes for discord_lumberjack-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 72522f7bee90060c9fe52e3b0ae9e445d2caa831179e9d4daffa89286e673cfc
MD5 2e0569e84c0f72e1a48313f921195de5
BLAKE2b-256 f44068373c5299dbc65127daaa82a247e3e784ae23efc94210d0629f4c7ccdbc

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