跳转到主要内容

Python日志框架的Discord处理器

项目描述

PyPI version

Automated test suite

Documentation Status

Python日志的Discord处理器

使用Python日志子系统和Discord Webhook库将Python日志输出重定向到Discord。

Example screenshot

用例

  • 轻松与非技术同事分享日志
  • 在服务器端错误上获得通知
  • 轻松跟踪批处理作业过程
  • 适合在Discord中设置消息的业务和社区

功能

  • 不需要或仅需对Python应用程序进行最小修改
  • 可选使用Discord嵌入对消息进行颜色编码
  • 可选在消息中使用Unicode表情符号
  • Discord友好的速率限制,适用于日志突发
  • 文档
  • 特别处理长日志消息(如堆栈跟踪),以处理Discord的最大2000个字符的消息长度

要求

  • Python 3.8+

安装

使用pip:

pip install python-logging-discord-handler

使用Poetry:

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!")

在examples.py源代码中查找更多示例.

如何获取Discord webhook URL

  1. 前往Discord中的编辑频道(齿轮)
  2. 选择集成
  3. 选择查看webhooks -> 新建
  4. 复制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 来拆分长消息

颜色和表情符号

日志消息可以装饰颜色和表情符号。

Emoji screenshot

以下是默认设置

#: 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 (9.5 kB 查看哈希值)

上传

构建分发

python_logging_discord_handler-0.1.4-py3-none-any.whl (9.5 kB 查看哈希值)

上传 Python 3

支持者