Skip to main content

Simple Discord webhook logger

Project description

Discord webhook logging

Simple logging handler and formatter for sending larger logs to discord channels.

Installation

Package can be installed via pip.

pip install discord-webhook-logging

Usage

import logging
from discord_webhook_logging import DiscordWebhookHandler

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
handler = DiscordWebhookHandler(webhook_url='<your webhook url>')
logger.addHandler(handler)

Examples

Here are some basic examples to demonstrate how this package works.

Example of all log levels

Python:

logger.debug('This is debug message')
logger.info('This is info message')
logger.warning('This is warning message')
logger.error('This is error message')
logger.critical('This is critical message')

Discord:

===│This is debug message
+  │This is info message
W  │This is warning message
-  │This is error message
-!!│This is critical message

Flushing buffered messages

DiscordWebhookHandler by default buffers messages, so it can send more lines as single messasge. Buffer can store 1989 due to Discords message length limits. It is automatically flushed when there is not enogh space for next message. You can also manualy flush the buffer by calling logger.flush().

Buffer is also automatically flushed right before the app exits.

Python:

logger.debug('This is debug message')
logger.info('This is info message')

logger.flush()  # All messages in buffer are sent

logger.warning('This is warning message')
logger.error('This is error message')
logger.critical('This is critical message')

# As you can see we do not need to use flush at the end of program.
# Remaining buffered messages are automatically flushed before the app exits.

Discord:

You will see two messages in Discord insted of one like in example above.

===│This is debug message
+  │This is info message
W  │This is warning message
-  │This is error message
-!!│This is critical message

If you want, you can set to flush buffer automatically after every message. However, I do not recommend doing it, due to Discords API rate limits. You can enable this by setting auto_flush=True while creating the handler.

handler = DiscordWebhookHandler(webhook_url='<your webhook url>', auto_flush=True)

In this case, after every message, handler sends message to discord channel.

Python:

logger.debug('This is debug message')
logger.info('This is info message')
logger.warning('This is warning message')

Discord:

===│This is debug message
+  │This is info message
W  │This is warning message

Multi line log

If single log string contains multiple lines, it is highlighted with "├" character from 2nd line and last line is highlighted with "└"

Python:

logger.error('1st line\nnext line\n3rd line\nlast line')

Discord:

-  │1st line
-  ├next line
-  ├3rd line
-  └last line

Long messages

Discord supports sending messages up to 2000 characters. If longer string is passed into logger, DiscordWebhookFormatter automatically splits message into shorter parts to fit discords 2000 character limt. If the line was split, it is highlighted with "↳" character.

Python:

logger.info('0'*2000 + '1'*50)

Discord:

+  │000000000000000000000000000 ... zeroes contiues up to message length limit

And in the next message remaining text from the same log line

+  ↳00000000000000011111111111111111111111111111111111111111111111111

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-webhook-logging-0.0.3.tar.gz (5.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_webhook_logging-0.0.3-py3.8.egg (7.0 kB view details)

Uploaded Egg

File details

Details for the file discord-webhook-logging-0.0.3.tar.gz.

File metadata

  • Download URL: discord-webhook-logging-0.0.3.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.9.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.2

File hashes

Hashes for discord-webhook-logging-0.0.3.tar.gz
Algorithm Hash digest
SHA256 254aa1b7454f24baa42a21e2df0eff244f965b013df32bc502f0b5d32a2bc806
MD5 58908b8047039ae646196c5d06c2cc58
BLAKE2b-256 a3233372bad78ce3af32d3d7f76ee7e53b210976eb8231bde36976e10cd1aac1

See more details on using hashes here.

File details

Details for the file discord_webhook_logging-0.0.3-py3.8.egg.

File metadata

  • Download URL: discord_webhook_logging-0.0.3-py3.8.egg
  • Upload date:
  • Size: 7.0 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.9.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.2

File hashes

Hashes for discord_webhook_logging-0.0.3-py3.8.egg
Algorithm Hash digest
SHA256 8d66c34416a2d17e52fbff4a1c5970a27b6df7e2a3d3d7308c92eb81c776ee0f
MD5 ca839dbfd8c5ed6a9fa23fc2e563ae6f
BLAKE2b-256 64408bbf59f69196eb3078afcf6f971d48f06294dbe3d3fe671e1f9298404c8a

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