A logging package for Discord bots
Project description
BotLogging
BotLogging is a logging package for Discord bots. It allows bot developers to log to Discord channels along with the standard terminal console, providing an easy way to track events and errors.
pip install botlogging
Logging Levels
The logging interface is designed to simulate the standard Python logging levels: debug
, info
, warning
, and error
.
Debug / Info / Warning
The first three levels by default do not send to Discord. However, setting send=True
provides this.
from discord.ext import commands
import botlogging
token = ""
bot = commands.Bot(token)
logger = botlogging.BotLogger(bot=bot, name="mybot")
logging_channel = 818657960038250216
@bot.command(name="echo")
async def echo(ctx, *, input: str):
await logger.info("Executing echo command", send=True, channel=logging_channel)
await ctx.send(content=input)
Error
For errors, the default is to send to Discord along with a traceback. Note, critical=True
will add a mention to the message, so the guild owner will be notified directly.
@bot.command(name="run")
async def run(ctx):
try:
await some_function()
except Exception as e:
await logger.error(
"Could not execute some_function!",
exception=e,
context=context,
channel=logging_channel,
critical=True
)
await ctx.send(content=input)
Console
If you just want to log to the standard logging console, you can still reference it with the bot logger. This is useful because it is not an async method and can be used in synchronous code.
def setup_bot_config(bot, logger):
logger.console.debug("Loading bot config")
# ...
Delayed logs
To avoid rate limiting issues, you can delay log events using a delayed logger.
from discord.ext import commands
import botlogging
logger = botlogging.DelayedLogger(bot=bot, name="mybot")
then within your event loop, run
logger.register_queue()
Custom embeds
You can override the embeds sent to Discord by passing in your own embeds.
# create a custom embed
embed = discord.Embed(description="Custom embed!")
embed.add_field(name="Some custom field", value="some custom value")
await logger.info("Executing echo command", send=True, channel=logging_channel, embed=embed)
Note: this still adds in base formatting to the embed such as color and title.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file botlogging-2.0.0.tar.gz
.
File metadata
- Download URL: botlogging-2.0.0.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0214c1b3e917833764c75e6bd051f6b55f08e3ad44f806611de94c0a4b0d657d |
|
MD5 | 2728cbb565762e4ba5a29d31a17bd678 |
|
BLAKE2b-256 | 262db8c6508be7f9f975349690a620cc4b44f15209c1ca5389cb691f61dc1ddb |
File details
Details for the file botlogging-2.0.0-py3-none-any.whl
.
File metadata
- Download URL: botlogging-2.0.0-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b001e6267d13e88bc857ecb15380e9def38377800e23c41a523f45829263b443 |
|
MD5 | 4d312899b0c4c8be3bbf10b2840e35f6 |
|
BLAKE2b-256 | 1754bec6eedbef7e7c08934d9d1a5bd76434b125bb80505912c069a203714b19 |