Skip to main content

项目级统一日志 SDK,提供标准字段、上下文注入、脱敏和多通道输出能力

Project description

tf-logging-sdk

项目级统一日志 SDK,提供统一初始化入口、标准字段、上下文注入、敏感信息脱敏,以及 stdout / file / Logstash TCP 多通道输出能力。

仓库地址:

功能特性

  1. 提供统一入口 setup_logging
  2. 提供 bind_log_contextclear_log_context
  3. 统一输出标准字段,便于检索和排障
  4. 默认脱敏 passwordtokenauthorizationcookiesecret 等敏感字段
  5. 支持控制台可读输出
  6. 支持 JSON 文件滚动日志
  7. 支持 TCP Logstash 结构化输出
  8. 未配置 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 默认统一输出以下字段:

  1. timestamp
  2. level
  3. service
  4. environment
  5. module
  6. message
  7. trace_id
  8. task_id
  9. space_id
  10. message_type
  11. topic
  12. consumer_group
  13. worker_type
  14. region
  15. duration_ms
  16. error_code
  17. error

输出方式

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 管理上下文,避免不同线程或异步任务串线。

脱敏说明

默认脱敏字段:

  1. password
  2. token
  3. authorization
  4. cookie
  5. secret
  6. access_key
  7. proxy_password
  8. phone
  9. email

扩展示例:

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

文档

  1. 统一日志SDK设计说明.md
  2. 统一日志SDK接入指南.md
  3. 统一日志SDK验收记录.md

本地验证

python -m compileall src/tf_logging_sdk
python -m tf_logging_sdk.examples.self_check

说明:

  1. self_check.py 用于验证标准字段、上下文注入、文件输出与脱敏结果
  2. 若按 src 布局直接执行模块,建议先执行 pip install -e .

Project details


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)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tf_logging_sdk-0.1.0-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

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

Hashes for tf_logging_sdk-0.1.0.tar.gz
Algorithm Hash digest
SHA256 06e3b6a71fd78d3eb9391421e098b97ed05f9620663f3c6bff929ac048c15bab
MD5 2a105686ea8f48a678ee000945cfd633
BLAKE2b-256 45dbbf2bc18efd822e39e7a345d10481d0a5b7f6ee158ae892d5cc34fa9dffd5

See more details on using hashes here.

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

Hashes for tf_logging_sdk-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5bdb3f7439078f569e7a2d25645d28516413f553f723f5a3fb83b54c6e9f26fd
MD5 353fb8193ebe78a60c78831520f12d9f
BLAKE2b-256 bdc3b208501c2310646cfa5e83bc9dc667e564db03dc31448f884fc3d9440821

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page