Python日志框架的Discord处理器
项目描述
Python日志的Discord处理器
使用Python日志子系统和Discord Webhook库将Python日志输出重定向到Discord。
用例
- 轻松与非技术同事分享日志
- 在服务器端错误上获得通知
- 轻松跟踪批处理作业过程
- 适合在Discord中设置消息的业务和社区
功能
- 不需要或仅需对Python应用程序进行最小修改
- 可选使用Discord嵌入对消息进行颜色编码
- 可选在消息中使用Unicode表情符号
- Discord友好的速率限制,适用于日志突发
- 文档
- 特别处理长日志消息(如堆栈跟踪),以处理Discord的最大2000个字符的消息长度
要求
- Python 3.8+
安装
pip install python-logging-discord-handler
poetry add python-logging-discord-handler
用法
此示例将日志同时记录到Discord和标准输出。
首先您需要
import logging
from discord_logging.handler import DiscordHandler
# See instructions below how to get a Webhook URL
webhook_url = # ...
logger = logging.getLogger()
stream_format = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
discord_format = logging.Formatter("%(message)s")
discord_handler = DiscordHandler(
"Hello World Bot",
webhook_url,
avatar_url="https://i0.wp.com/www.theterminatorfans.com/wp-content/uploads/2012/09/the-terminator3.jpg?resize=900%2C450&ssl=1")
#discord_handler = DiscordHandler("Happy Bot", webhook_url, emojis={})
discord_handler.setFormatter(discord_format)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(stream_format)
# Add the handlers to the Logger
logger.addHandler(discord_handler)
logger.addHandler(stream_handler)
logger.setLevel(logging.DEBUG)
logger.info("This is an info message")
logger.debug("A debug message - usually not that interesting")
logger.error("Very nasty error messgae!")
如何获取Discord webhook URL
- 前往Discord中的编辑频道(齿轮)
- 选择集成
- 选择查看webhooks -> 新建
- 复制URL
Webhook URL安全
建议您将 webhook URL 存储在源代码之外,以避免在黑客攻击或其他安全事件中损坏。
在 Linux/macOS 命令行中,您可以这样做
export DISCORD_TEST_WEBHOOK_URL=<your webhook URL here>
对于长期配置,您可以在源代码树之外创建一个文件来存储环境变量,例如在您的家目录中。将 export
命令存储在那里。
# Text editor for a secrets.env file in your home directory on Linux
nano ~/secrets.env
在您的 Linux shell 会话中,您可以使用 shell 中的 source 命令读取此文件,并使环境变量生效
# Reads secrets.env and executes all commands there and makes them effective
# in the current shell session
source ~/secrets.env
然后您可以在 Python 代码中读取环境变量
import os
webhook_url = os.environ["DISCORD_TEST_WEBHOOK_URL"]
Discord 限制
- 每条消息最多 2000 个字符。查看 API 文档了解如何使用不同选项绕过此限制。默认情况下,日志消息的最后一行(如跟踪信息)将显示。
- Discord 嵌入,那些在左侧提供日志级别颜色条的,在处理长行时非常困难。默认情况下,对于长行禁用嵌入。
日志输出格式化逻辑
日志消息将按照以下逻辑转换为 Discord 嵌入
- 单行日志消息将转换为嵌入标题
- 对于多行日志消息,第一行是嵌入标题,以下行是嵌入描述
- 长行或长消息无法转换为嵌入,而是使用 Discord Markdown 代码格式化 来保留输出可读性
- 可以手动分配一个特殊的
message_break_char
来拆分长消息
颜色和表情符号
日志消息可以装饰颜色和表情符号。
以下是默认设置
#: The default log level colors as hexacimal, converted int
DEFAULT_COLOURS = {
None: 2040357, # Unknown log level
logging.CRITICAL: 14362664, # Red
logging.ERROR: 14362664, # Red
logging.WARNING: 16497928, # Yellow
logging.INFO: 2196944, # Blue
logging.DEBUG: 8947848, # Gray
}
#: The default log emojis as
DEFAULT_EMOJIS = {
None: "", # Unknown log level
logging.CRITICAL: "🆘",
logging.ERROR: "❌",
logging.WARNING: "⚠️",
logging.INFO: "",
logging.DEBUG: "",
}
默认情况下禁用表情符号,因为它们经常使输出过于鲜艳,难以阅读。
测试和开发
手动测试
检查日志输出在 Discord 中的外观。
- 检出此 Git 仓库
- 设置一个虚拟 Discord 频道
- 获取其 webhook URL
poetry install -E docs
export DISCORD_TEST_WEBHOOK_URL=...
python discord_logging/examples.py
这将在您的 Discord 中输出一些消息。
自动测试
运行
pytest
历史记录
最初为交易策略创建 以跟踪交易机器人活动。
许可
MIT
项目详情
关闭
python_logging_discord_handler-0.1.4.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 8bfa839b6503b3b87e5851dd13bc5ff80bf2fadb496ac22c338ac10bd926f75a |
|
MD5 | 24af6e8c6b7229fdb3b47f03b53322c9 |
|
BLAKE2b-256 | a294c67d7d1bd268ed3f3dd6775905ea8523ef38a6ecd599d563367e947e75fb |
关闭
python_logging_discord_handler-0.1.4-py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | b804b48e3f5af8c9c781a9afe8243c806f01521662e38a60fcda2c3631d27f4f |
|
MD5 | 5e0f490ccb98c5022b398458a6f2c369 |
|
BLAKE2b-256 | d179669464da149b3a3b15b4f0d17e5ebd548d44c466b644a7b610ebf4987092 |