yai-loguru-sinks
企业级 Loguru Sink 工厂库,基于 loguru-config 提供统一的配置驱动体验。
核心理念
- 简洁架构:移除复杂的插件抽象,直接提供 sink 工厂函数
- 配置驱动:基于
loguru-config的统一配置体验 - 企业级:专注于阿里云 SLS 日志服务,未来扩展更多云服务
- 原生兼容:完全兼容 Loguru 的 sink 机制
快速开始
安装
uv add yai-loguru-sinks
基本使用
from yai_loguru_sinks import register_protocol_parsers, create_config_from_file
# 注册企业级协议解析器
register_protocol_parsers()
# 配置驱动,一行搞定
create_config_from_file('logging.yaml')
配置文件示例
# logging.yaml
handlers:
# 阿里云 SLS
- sink: sls://my-project/my-logstore?region=cn-hangzhou&access_key_id=${SLS_ACCESS_KEY}&access_key_secret=${SLS_SECRET}
level: WARNING
format: '{time} | {level} | {message}'
# 本地文件(Loguru 原生)
- sink: logs/app.log
rotation: "1 day"
retention: "30 days"
compression: "gz"
支持的协议
阿里云 SLS
sink: sls://project/logstore?region=cn-hangzhou&access_key_id=xxx&access_key_secret=xxx
参数说明:
project: SLS 项目名称logstore: SLS 日志库名称region: 阿里云地域(如 cn-hangzhou)access_key_id: 阿里云访问密钥 IDaccess_key_secret: 阿里云访问密钥 Secret
环境变量支持:
sink: sls://my-project/my-logstore?region=cn-hangzhou&access_key_id=${SLS_ACCESS_KEY}&access_key_secret=${SLS_SECRET}
API 参考
核心函数
register_protocol_parsers()
注册所有支持的协议解析器(SLS、CloudWatch、Elasticsearch、Kafka 等)。
from yai_loguru_sinks import register_protocol_parsers
# 注册协议解析器
register_protocol_parsers()
create_config_from_file(config_path: str)
从 YAML/JSON 配置文件加载日志配置。
from yai_loguru_sinks import create_config_from_file
# 从文件加载配置
create_config_from_file('logging.yaml')
create_config_from_dict(config_dict: dict)
从字典对象加载日志配置。
from yai_loguru_sinks import create_config_from_dict
config = {
"handlers": [
{
"sink": "sls://my-project/my-logstore?region=cn-hangzhou",
"level": "INFO"
}
]
}
create_config_from_dict(config)
直接使用 Sink 工厂
如果不使用配置文件,也可以直接调用内部的 sink 工厂:
from loguru import logger
from yai_loguru_sinks.internal.factory import create_sls_sink
# 创建 SLS sink
sls_sink = create_sls_sink(
project="my-project",
logstore="my-logstore",
region="cn-hangzhou",
access_key_id="xxx",
access_key_secret="xxx"
)
# 添加到 logger
logger.add(sls_sink, level="WARNING")
特性功能
PackId 支持
自动为每个日志记录生成唯一的 PackId,便于日志关联和追踪。
异步处理
高性能异步日志发送,不阻塞主线程。
优雅降级
当云服务不可用时,自动降级到本地文件日志。
环境变量支持
配置文件中可以使用 ${VAR_NAME} 语法引用环境变量。
架构优势
相比传统插件系统
- ✅ 移除冗余抽象:不再需要
LoguruPlugin基类 - ✅ 简化使用方式:统一的配置文件格式
- ✅ 利用成熟生态:基于
loguru-config的稳定基础 - ✅ 保持功能完整:所有企业级功能都保留
设计原则
- 职责分离:
loguru负责基础功能,yai-loguru-sinks负责企业级扩展 - 配置驱动:通过 YAML/JSON 配置,而非代码硬编码
- 协议扩展:利用
loguru-config的协议解析机制 - 工厂模式:提供灵活的 sink 创建函数
许可证
MIT License
Metadata
Release files for yai-loguru-sinks 0.6.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| yai_loguru_sinks-0.6.3.tar.gz | 121.0 kB | Without provenance | Details for yai_loguru_sinks-0.6.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| yai_loguru_sinks-0.6.3-py3-none-any.whl | Python 3 | none | any | Details for yai_loguru_sinks-0.6.3-py3-none-any.whl |
Total release size: 137.8 kB
Release files / yai_loguru_sinks-0.6.3.tar.gz
| Download URL | yai_loguru_sinks-0.6.3.tar.gz |
|---|---|
| Size | 121.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
dc5cc97cffa8f5d52a588be53eb2df69567552b9052bf07939a1fc2819104ba8
|
|
BLAKE2b-256 checksum How to use checksums |
c2830df6187e7d060974a449359a8a4abd19ed9d789c3b7fec79b09e3d18acce
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.3
|
Release files / yai_loguru_sinks-0.6.3-py3-none-any.whl
| Download URL | yai_loguru_sinks-0.6.3-py3-none-any.whl |
|---|---|
| Size | 16.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6ad1fb1cab54d57946d80c938cbb56ffa7fa23b9416b02c1a9ca0a862ea95ea1
|
|
BLAKE2b-256 checksum How to use checksums |
27320e44853a62ab41871488be83435055baaa7a91f26d932ca552668797c3f5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.3
|
Security for yai-loguru-sinks 0.6.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.