项目级统一日志 SDK,提供标准字段、上下文注入、脱敏和多通道输出能力
Project description
tf-logging-sdk
项目级统一日志 SDK,提供统一初始化入口、标准字段、上下文注入、敏感信息脱敏,以及 stdout / file / Logstash TCP 多通道输出能力。
仓库地址:
- GitHub: https://github.com/tfkxsx/tf-logging-sdk
- Git:
git@github.com:tfkxsx/tf-logging-sdk.git
功能特性
- 提供统一入口
setup_logging - 提供
bind_log_context和clear_log_context - 统一输出标准字段,便于检索和排障
- 默认脱敏
password、token、authorization、cookie、secret等敏感字段 - 支持控制台可读输出
- 支持 JSON 文件滚动日志
- 支持 TCP Logstash 结构化输出
- 未配置 Logstash 时自动降级,不影响本地启动
安装
pip install tf-logging-sdk
本地开发安装:
pip install -e .[dev]
快速开始
import logging
from tf_logging_sdk import bind_log_context, clear_log_context, setup_logging
setup_logging(
service_name="kafka-task",
environment="dev",
enable_file=True,
log_file_path="logs/kafka-task.log",
enable_logstash=False,
)
logger = logging.getLogger(__name__)
bind_log_context(
trace_id="trace-demo-001",
task_id="task-demo-001",
space_id="space-demo-001",
topic="task.dispatch",
message_type="task.created",
)
logger.info(
"开始处理任务",
extra={
"duration_ms": 12,
"request_payload": {
"token": "abcd1234567890",
"email": "demo@example.com",
},
},
)
clear_log_context()
标准字段
SDK 默认统一输出以下字段:
timestamplevelserviceenvironmentmodulemessagetrace_idtask_idspace_idmessage_typetopicconsumer_groupworker_typeregionduration_mserror_codeerror
输出方式
1. 控制台输出
setup_logging(
service_name="schedule-task",
environment="dev",
)
2. 文件日志
setup_logging(
service_name="extractors",
environment="dev",
enable_file=True,
log_file_path="logs/extractors.log",
file_max_bytes=50 * 1024 * 1024,
file_backup_count=5,
)
3. Logstash 输出
setup_logging(
service_name="tf-data",
environment="prod",
enable_logstash=True,
logstash_host="127.0.0.1",
logstash_port=5959,
)
上下文注入
from tf_logging_sdk import bind_log_context, clear_log_context
bind_log_context(
trace_id="trace-001",
task_id="task-001",
topic="task.dispatch",
message_type="task.created",
consumer_group="group-task",
worker_type="consumer",
)
clear_log_context()
SDK 基于 contextvars 管理上下文,避免不同线程或异步任务串线。
脱敏说明
默认脱敏字段:
passwordtokenauthorizationcookiesecretaccess_keyproxy_passwordphoneemail
扩展示例:
setup_logging(
service_name="demo-service",
environment="test",
extra_mask_fields=["session_id", "private_key"],
)
项目结构
tf_logging_sdk/
├── examples/
├── src/
│ └── tf_logging_sdk/
├── LICENSE
├── MANIFEST.in
├── README.md
├── pyproject.toml
├── 统一日志SDK设计说明.md
├── 统一日志SDK接入指南.md
└── 统一日志SDK验收记录.md
文档
本地验证
python -m compileall src/tf_logging_sdk
python -m tf_logging_sdk.examples.self_check
说明:
self_check.py用于验证标准字段、上下文注入、文件输出与脱敏结果- 若按
src布局直接执行模块,建议先执行pip install -e .
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
tf_logging_sdk-0.1.0.tar.gz
(12.8 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tf_logging_sdk-0.1.0.tar.gz.
File metadata
- Download URL: tf_logging_sdk-0.1.0.tar.gz
- Upload date:
- Size: 12.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
06e3b6a71fd78d3eb9391421e098b97ed05f9620663f3c6bff929ac048c15bab
|
|
| MD5 |
2a105686ea8f48a678ee000945cfd633
|
|
| BLAKE2b-256 |
45dbbf2bc18efd822e39e7a345d10481d0a5b7f6ee158ae892d5cc34fa9dffd5
|
File details
Details for the file tf_logging_sdk-0.1.0-py3-none-any.whl.
File metadata
- Download URL: tf_logging_sdk-0.1.0-py3-none-any.whl
- Upload date:
- Size: 12.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5bdb3f7439078f569e7a2d25645d28516413f553f723f5a3fb83b54c6e9f26fd
|
|
| MD5 |
353fb8193ebe78a60c78831520f12d9f
|
|
| BLAKE2b-256 |
bdc3b208501c2310646cfa5e83bc9dc667e564db03dc31448f884fc3d9440821
|