Simple logging
handler and formatter for sending larger logs to discord channels.
Package can be installed via pip.
pip install discord-webhook-logging
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)
Here are some basic examples to demonstrate how this package works.
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')
===│This is debug message + │This is info message W │This is warning message - │This is error message -!!│This is critical message
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.
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.
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.
logger.debug('This is debug message')
logger.info('This is info message')
logger.warning('This is warning message')
===│This is debug message
+ │This is info message
W │This is warning message
If single log string contains multiple lines, it is highlighted with "├" character from 2nd line and last line is highlighted with "└"
logger.error('1st line\nnext line\n3rd line\nlast line')
- │1st line
- ├next line
- ├3rd line
- └last line
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.
logger.info('0'*2000 + '1'*50)
+ │000000000000000000000000000 ... zeroes contiues up to message length limit
And in the next message remaining text from the same log line
+ ↳00000000000000011111111111111111111111111111111111111111111111111