tf-logging-sdk 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ *.pyo
4
+ *.so
5
+ .DS_Store
6
+ .pytest_cache/
7
+ .mypy_cache/
8
+ .ruff_cache/
9
+ .coverage
10
+ htmlcov/
11
+ dist/
12
+ build/
13
+ *.egg-info/
14
+ .venv/
15
+ venv/
16
+ env/
17
+ logs/
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 tfkxsx
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,10 @@
1
+ include README.md
2
+ include LICENSE
3
+ include pyproject.toml
4
+ include 统一日志SDK设计说明.md
5
+ include 统一日志SDK接入指南.md
6
+ include 统一日志SDK验收记录.md
7
+ recursive-include src/tf_logging_sdk *.py
8
+ recursive-include examples *.py
9
+ global-exclude __pycache__/*
10
+ global-exclude *.py[cod]
@@ -0,0 +1,234 @@
1
+ Metadata-Version: 2.4
2
+ Name: tf-logging-sdk
3
+ Version: 0.1.0
4
+ Summary: 项目级统一日志 SDK,提供标准字段、上下文注入、脱敏和多通道输出能力
5
+ Project-URL: Homepage, https://github.com/tfkxsx/tf-logging-sdk
6
+ Project-URL: Repository, https://github.com/tfkxsx/tf-logging-sdk
7
+ Project-URL: Issues, https://github.com/tfkxsx/tf-logging-sdk/issues
8
+ Project-URL: Source, https://github.com/tfkxsx/tf-logging-sdk.git
9
+ Author-email: tfkxsx <tfkxsx@gmail.com>
10
+ License: MIT
11
+ License-File: LICENSE
12
+ Keywords: logging,logstash,observability,sdk,structured-logging
13
+ Classifier: Development Status :: 4 - Beta
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: License :: OSI Approved :: MIT License
16
+ Classifier: Operating System :: OS Independent
17
+ Classifier: Programming Language :: Python :: 3
18
+ Classifier: Programming Language :: Python :: 3.10
19
+ Classifier: Programming Language :: Python :: 3.11
20
+ Classifier: Programming Language :: Python :: 3.12
21
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
22
+ Classifier: Topic :: System :: Logging
23
+ Requires-Python: >=3.10
24
+ Provides-Extra: dev
25
+ Requires-Dist: build>=1.2.1; extra == 'dev'
26
+ Requires-Dist: pytest>=8.0.0; extra == 'dev'
27
+ Requires-Dist: twine>=5.0.0; extra == 'dev'
28
+ Description-Content-Type: text/markdown
29
+
30
+ # tf-logging-sdk
31
+
32
+ 项目级统一日志 SDK,提供统一初始化入口、标准字段、上下文注入、敏感信息脱敏,以及 `stdout / file / Logstash TCP` 多通道输出能力。
33
+
34
+ 仓库地址:
35
+
36
+ - GitHub: [https://github.com/tfkxsx/tf-logging-sdk](https://github.com/tfkxsx/tf-logging-sdk)
37
+ - Git: `git@github.com:tfkxsx/tf-logging-sdk.git`
38
+
39
+ ## 功能特性
40
+
41
+ 1. 提供统一入口 `setup_logging`
42
+ 2. 提供 `bind_log_context` 和 `clear_log_context`
43
+ 3. 统一输出标准字段,便于检索和排障
44
+ 4. 默认脱敏 `password`、`token`、`authorization`、`cookie`、`secret` 等敏感字段
45
+ 5. 支持控制台可读输出
46
+ 6. 支持 JSON 文件滚动日志
47
+ 7. 支持 TCP Logstash 结构化输出
48
+ 8. 未配置 Logstash 时自动降级,不影响本地启动
49
+
50
+ ## 安装
51
+
52
+ ```bash
53
+ pip install tf-logging-sdk
54
+ ```
55
+
56
+ 本地开发安装:
57
+
58
+ ```bash
59
+ pip install -e .[dev]
60
+ ```
61
+
62
+ ## 快速开始
63
+
64
+ ```python
65
+ import logging
66
+
67
+ from tf_logging_sdk import bind_log_context, clear_log_context, setup_logging
68
+
69
+ setup_logging(
70
+ service_name="kafka-task",
71
+ environment="dev",
72
+ enable_file=True,
73
+ log_file_path="logs/kafka-task.log",
74
+ enable_logstash=False,
75
+ )
76
+
77
+ logger = logging.getLogger(__name__)
78
+
79
+ bind_log_context(
80
+ trace_id="trace-demo-001",
81
+ task_id="task-demo-001",
82
+ space_id="space-demo-001",
83
+ topic="task.dispatch",
84
+ message_type="task.created",
85
+ )
86
+
87
+ logger.info(
88
+ "开始处理任务",
89
+ extra={
90
+ "duration_ms": 12,
91
+ "request_payload": {
92
+ "token": "abcd1234567890",
93
+ "email": "demo@example.com",
94
+ },
95
+ },
96
+ )
97
+
98
+ clear_log_context()
99
+ ```
100
+
101
+ ## 标准字段
102
+
103
+ SDK 默认统一输出以下字段:
104
+
105
+ 1. `timestamp`
106
+ 2. `level`
107
+ 3. `service`
108
+ 4. `environment`
109
+ 5. `module`
110
+ 6. `message`
111
+ 7. `trace_id`
112
+ 8. `task_id`
113
+ 9. `space_id`
114
+ 10. `message_type`
115
+ 11. `topic`
116
+ 12. `consumer_group`
117
+ 13. `worker_type`
118
+ 14. `region`
119
+ 15. `duration_ms`
120
+ 16. `error_code`
121
+ 17. `error`
122
+
123
+ ## 输出方式
124
+
125
+ ### 1. 控制台输出
126
+
127
+ ```python
128
+ setup_logging(
129
+ service_name="schedule-task",
130
+ environment="dev",
131
+ )
132
+ ```
133
+
134
+ ### 2. 文件日志
135
+
136
+ ```python
137
+ setup_logging(
138
+ service_name="extractors",
139
+ environment="dev",
140
+ enable_file=True,
141
+ log_file_path="logs/extractors.log",
142
+ file_max_bytes=50 * 1024 * 1024,
143
+ file_backup_count=5,
144
+ )
145
+ ```
146
+
147
+ ### 3. Logstash 输出
148
+
149
+ ```python
150
+ setup_logging(
151
+ service_name="tf-data",
152
+ environment="prod",
153
+ enable_logstash=True,
154
+ logstash_host="127.0.0.1",
155
+ logstash_port=5959,
156
+ )
157
+ ```
158
+
159
+ ## 上下文注入
160
+
161
+ ```python
162
+ from tf_logging_sdk import bind_log_context, clear_log_context
163
+
164
+ bind_log_context(
165
+ trace_id="trace-001",
166
+ task_id="task-001",
167
+ topic="task.dispatch",
168
+ message_type="task.created",
169
+ consumer_group="group-task",
170
+ worker_type="consumer",
171
+ )
172
+
173
+ clear_log_context()
174
+ ```
175
+
176
+ SDK 基于 `contextvars` 管理上下文,避免不同线程或异步任务串线。
177
+
178
+ ## 脱敏说明
179
+
180
+ 默认脱敏字段:
181
+
182
+ 1. `password`
183
+ 2. `token`
184
+ 3. `authorization`
185
+ 4. `cookie`
186
+ 5. `secret`
187
+ 6. `access_key`
188
+ 7. `proxy_password`
189
+ 8. `phone`
190
+ 9. `email`
191
+
192
+ 扩展示例:
193
+
194
+ ```python
195
+ setup_logging(
196
+ service_name="demo-service",
197
+ environment="test",
198
+ extra_mask_fields=["session_id", "private_key"],
199
+ )
200
+ ```
201
+
202
+ ## 项目结构
203
+
204
+ ```text
205
+ tf_logging_sdk/
206
+ ├── examples/
207
+ ├── src/
208
+ │ └── tf_logging_sdk/
209
+ ├── LICENSE
210
+ ├── MANIFEST.in
211
+ ├── README.md
212
+ ├── pyproject.toml
213
+ ├── 统一日志SDK设计说明.md
214
+ ├── 统一日志SDK接入指南.md
215
+ └── 统一日志SDK验收记录.md
216
+ ```
217
+
218
+ ## 文档
219
+
220
+ 1. [统一日志SDK设计说明.md](./统一日志SDK设计说明.md)
221
+ 2. [统一日志SDK接入指南.md](./统一日志SDK接入指南.md)
222
+ 3. [统一日志SDK验收记录.md](./统一日志SDK验收记录.md)
223
+
224
+ ## 本地验证
225
+
226
+ ```bash
227
+ python -m compileall src/tf_logging_sdk
228
+ python -m tf_logging_sdk.examples.self_check
229
+ ```
230
+
231
+ 说明:
232
+
233
+ 1. `self_check.py` 用于验证标准字段、上下文注入、文件输出与脱敏结果
234
+ 2. 若按 `src` 布局直接执行模块,建议先执行 `pip install -e .`
@@ -0,0 +1,205 @@
1
+ # tf-logging-sdk
2
+
3
+ 项目级统一日志 SDK,提供统一初始化入口、标准字段、上下文注入、敏感信息脱敏,以及 `stdout / file / Logstash TCP` 多通道输出能力。
4
+
5
+ 仓库地址:
6
+
7
+ - GitHub: [https://github.com/tfkxsx/tf-logging-sdk](https://github.com/tfkxsx/tf-logging-sdk)
8
+ - Git: `git@github.com:tfkxsx/tf-logging-sdk.git`
9
+
10
+ ## 功能特性
11
+
12
+ 1. 提供统一入口 `setup_logging`
13
+ 2. 提供 `bind_log_context` 和 `clear_log_context`
14
+ 3. 统一输出标准字段,便于检索和排障
15
+ 4. 默认脱敏 `password`、`token`、`authorization`、`cookie`、`secret` 等敏感字段
16
+ 5. 支持控制台可读输出
17
+ 6. 支持 JSON 文件滚动日志
18
+ 7. 支持 TCP Logstash 结构化输出
19
+ 8. 未配置 Logstash 时自动降级,不影响本地启动
20
+
21
+ ## 安装
22
+
23
+ ```bash
24
+ pip install tf-logging-sdk
25
+ ```
26
+
27
+ 本地开发安装:
28
+
29
+ ```bash
30
+ pip install -e .[dev]
31
+ ```
32
+
33
+ ## 快速开始
34
+
35
+ ```python
36
+ import logging
37
+
38
+ from tf_logging_sdk import bind_log_context, clear_log_context, setup_logging
39
+
40
+ setup_logging(
41
+ service_name="kafka-task",
42
+ environment="dev",
43
+ enable_file=True,
44
+ log_file_path="logs/kafka-task.log",
45
+ enable_logstash=False,
46
+ )
47
+
48
+ logger = logging.getLogger(__name__)
49
+
50
+ bind_log_context(
51
+ trace_id="trace-demo-001",
52
+ task_id="task-demo-001",
53
+ space_id="space-demo-001",
54
+ topic="task.dispatch",
55
+ message_type="task.created",
56
+ )
57
+
58
+ logger.info(
59
+ "开始处理任务",
60
+ extra={
61
+ "duration_ms": 12,
62
+ "request_payload": {
63
+ "token": "abcd1234567890",
64
+ "email": "demo@example.com",
65
+ },
66
+ },
67
+ )
68
+
69
+ clear_log_context()
70
+ ```
71
+
72
+ ## 标准字段
73
+
74
+ SDK 默认统一输出以下字段:
75
+
76
+ 1. `timestamp`
77
+ 2. `level`
78
+ 3. `service`
79
+ 4. `environment`
80
+ 5. `module`
81
+ 6. `message`
82
+ 7. `trace_id`
83
+ 8. `task_id`
84
+ 9. `space_id`
85
+ 10. `message_type`
86
+ 11. `topic`
87
+ 12. `consumer_group`
88
+ 13. `worker_type`
89
+ 14. `region`
90
+ 15. `duration_ms`
91
+ 16. `error_code`
92
+ 17. `error`
93
+
94
+ ## 输出方式
95
+
96
+ ### 1. 控制台输出
97
+
98
+ ```python
99
+ setup_logging(
100
+ service_name="schedule-task",
101
+ environment="dev",
102
+ )
103
+ ```
104
+
105
+ ### 2. 文件日志
106
+
107
+ ```python
108
+ setup_logging(
109
+ service_name="extractors",
110
+ environment="dev",
111
+ enable_file=True,
112
+ log_file_path="logs/extractors.log",
113
+ file_max_bytes=50 * 1024 * 1024,
114
+ file_backup_count=5,
115
+ )
116
+ ```
117
+
118
+ ### 3. Logstash 输出
119
+
120
+ ```python
121
+ setup_logging(
122
+ service_name="tf-data",
123
+ environment="prod",
124
+ enable_logstash=True,
125
+ logstash_host="127.0.0.1",
126
+ logstash_port=5959,
127
+ )
128
+ ```
129
+
130
+ ## 上下文注入
131
+
132
+ ```python
133
+ from tf_logging_sdk import bind_log_context, clear_log_context
134
+
135
+ bind_log_context(
136
+ trace_id="trace-001",
137
+ task_id="task-001",
138
+ topic="task.dispatch",
139
+ message_type="task.created",
140
+ consumer_group="group-task",
141
+ worker_type="consumer",
142
+ )
143
+
144
+ clear_log_context()
145
+ ```
146
+
147
+ SDK 基于 `contextvars` 管理上下文,避免不同线程或异步任务串线。
148
+
149
+ ## 脱敏说明
150
+
151
+ 默认脱敏字段:
152
+
153
+ 1. `password`
154
+ 2. `token`
155
+ 3. `authorization`
156
+ 4. `cookie`
157
+ 5. `secret`
158
+ 6. `access_key`
159
+ 7. `proxy_password`
160
+ 8. `phone`
161
+ 9. `email`
162
+
163
+ 扩展示例:
164
+
165
+ ```python
166
+ setup_logging(
167
+ service_name="demo-service",
168
+ environment="test",
169
+ extra_mask_fields=["session_id", "private_key"],
170
+ )
171
+ ```
172
+
173
+ ## 项目结构
174
+
175
+ ```text
176
+ tf_logging_sdk/
177
+ ├── examples/
178
+ ├── src/
179
+ │ └── tf_logging_sdk/
180
+ ├── LICENSE
181
+ ├── MANIFEST.in
182
+ ├── README.md
183
+ ├── pyproject.toml
184
+ ├── 统一日志SDK设计说明.md
185
+ ├── 统一日志SDK接入指南.md
186
+ └── 统一日志SDK验收记录.md
187
+ ```
188
+
189
+ ## 文档
190
+
191
+ 1. [统一日志SDK设计说明.md](./统一日志SDK设计说明.md)
192
+ 2. [统一日志SDK接入指南.md](./统一日志SDK接入指南.md)
193
+ 3. [统一日志SDK验收记录.md](./统一日志SDK验收记录.md)
194
+
195
+ ## 本地验证
196
+
197
+ ```bash
198
+ python -m compileall src/tf_logging_sdk
199
+ python -m tf_logging_sdk.examples.self_check
200
+ ```
201
+
202
+ 说明:
203
+
204
+ 1. `self_check.py` 用于验证标准字段、上下文注入、文件输出与脱敏结果
205
+ 2. 若按 `src` 布局直接执行模块,建议先执行 `pip install -e .`
@@ -0,0 +1,40 @@
1
+ """统一日志 SDK 最小接入示例。"""
2
+
3
+ from __future__ import annotations
4
+
5
+ import logging
6
+
7
+ from tf_logging_sdk import bind_log_context, clear_log_context, setup_logging
8
+
9
+
10
+ def main() -> None:
11
+ """演示 SDK 的基本接入方式。"""
12
+ setup_logging(
13
+ service_name="example-service",
14
+ environment="dev",
15
+ enable_file=True,
16
+ log_file_path="logs/example-service.log",
17
+ )
18
+
19
+ logger = logging.getLogger(__name__)
20
+ bind_log_context(
21
+ trace_id="trace-example-001",
22
+ task_id="task-example-001",
23
+ topic="example.topic",
24
+ message_type="example.created",
25
+ )
26
+ logger.info(
27
+ "示例任务开始处理",
28
+ extra={
29
+ "duration_ms": 20,
30
+ "payload": {
31
+ "password": "example-password",
32
+ "email": "demo@example.com",
33
+ },
34
+ },
35
+ )
36
+ clear_log_context()
37
+
38
+
39
+ if __name__ == "__main__":
40
+ main()
@@ -0,0 +1,52 @@
1
+ """统一日志 SDK 本地自检脚本。"""
2
+
3
+ from __future__ import annotations
4
+
5
+ import json
6
+ import logging
7
+ from pathlib import Path
8
+
9
+ from tf_logging_sdk import bind_log_context, clear_log_context, setup_logging
10
+
11
+
12
+ def main() -> None:
13
+ """执行基础能力自检。"""
14
+ log_file_path = Path("logs/tf_logging_sdk_self_check.log")
15
+ setup_logging(
16
+ service_name="tf-logging-sdk-self-check",
17
+ environment="dev",
18
+ enable_file=True,
19
+ log_file_path=str(log_file_path),
20
+ enable_logstash=True,
21
+ logstash_host=None,
22
+ logstash_port=None,
23
+ )
24
+
25
+ logger = logging.getLogger(__name__)
26
+ bind_log_context(
27
+ trace_id="trace-self-check-001",
28
+ task_id="task-self-check-001",
29
+ topic="sdk.self.check",
30
+ message_type="self.check",
31
+ worker_type="unit",
32
+ )
33
+ logger.info(
34
+ "开始执行 SDK 自检",
35
+ extra={
36
+ "duration_ms": 10,
37
+ "request_data": {
38
+ "password": "super-secret-password",
39
+ "token": "token-value-1234567890",
40
+ "email": "sdk-check@example.com",
41
+ },
42
+ },
43
+ )
44
+ clear_log_context()
45
+
46
+ log_lines = log_file_path.read_text(encoding="utf-8").strip().splitlines()
47
+ last_payload = json.loads(log_lines[-1])
48
+ print(json.dumps(last_payload, ensure_ascii=False, indent=2))
49
+
50
+
51
+ if __name__ == "__main__":
52
+ main()
@@ -0,0 +1,73 @@
1
+ [build-system]
2
+ requires = ["hatchling>=1.24.0"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "tf-logging-sdk"
7
+ version = "0.1.0"
8
+ description = "项目级统一日志 SDK,提供标准字段、上下文注入、脱敏和多通道输出能力"
9
+ readme = "README.md"
10
+ requires-python = ">=3.10"
11
+ license = {text = "MIT"}
12
+ authors = [
13
+ {name = "tfkxsx", email = "tfkxsx@gmail.com"},
14
+ ]
15
+ keywords = [
16
+ "logging",
17
+ "logstash",
18
+ "structured-logging",
19
+ "observability",
20
+ "sdk",
21
+ ]
22
+ classifiers = [
23
+ "Development Status :: 4 - Beta",
24
+ "Intended Audience :: Developers",
25
+ "License :: OSI Approved :: MIT License",
26
+ "Operating System :: OS Independent",
27
+ "Programming Language :: Python :: 3",
28
+ "Programming Language :: Python :: 3.10",
29
+ "Programming Language :: Python :: 3.11",
30
+ "Programming Language :: Python :: 3.12",
31
+ "Topic :: Software Development :: Libraries :: Python Modules",
32
+ "Topic :: System :: Logging",
33
+ ]
34
+ dependencies = []
35
+
36
+ [project.optional-dependencies]
37
+ dev = [
38
+ "build>=1.2.1",
39
+ "pytest>=8.0.0",
40
+ "twine>=5.0.0",
41
+ ]
42
+
43
+ [project.urls]
44
+ Homepage = "https://github.com/tfkxsx/tf-logging-sdk"
45
+ Repository = "https://github.com/tfkxsx/tf-logging-sdk"
46
+ Issues = "https://github.com/tfkxsx/tf-logging-sdk/issues"
47
+ Source = "https://github.com/tfkxsx/tf-logging-sdk.git"
48
+
49
+ [tool.hatch.build]
50
+ exclude = [
51
+ "/.git",
52
+ "/.venv",
53
+ "/logs",
54
+ "/__pycache__",
55
+ "*.pyc",
56
+ "*.pyo",
57
+ ]
58
+
59
+ [tool.hatch.build.targets.wheel]
60
+ packages = ["src/tf_logging_sdk"]
61
+
62
+ [tool.hatch.build.targets.sdist]
63
+ include = [
64
+ "pyproject.toml",
65
+ "README.md",
66
+ "LICENSE",
67
+ "MANIFEST.in",
68
+ "src/tf_logging_sdk/**",
69
+ "examples/**",
70
+ "统一日志SDK设计说明.md",
71
+ "统一日志SDK接入指南.md",
72
+ "统一日志SDK验收记录.md",
73
+ ]
@@ -0,0 +1,15 @@
1
+ """统一日志 SDK 对外导出入口。"""
2
+
3
+ from tf_logging_sdk.config import DEFAULT_MASK_FIELDS, DEFAULT_STANDARD_FIELDS, LoggingConfig
4
+ from tf_logging_sdk.context import bind_log_context, clear_log_context, get_log_context
5
+ from tf_logging_sdk.handlers import setup_logging
6
+
7
+ __all__ = [
8
+ "DEFAULT_MASK_FIELDS",
9
+ "DEFAULT_STANDARD_FIELDS",
10
+ "LoggingConfig",
11
+ "bind_log_context",
12
+ "clear_log_context",
13
+ "get_log_context",
14
+ "setup_logging",
15
+ ]