Python Logging
Python 3.11 及以上,无第三方运行依赖;使用标准库 logging,格式与级别由应用选择。
安装
示例适用于 0.5.1,当前发布状态见发布矩阵。
python -m pip install stellarmesh-logging==0.5.1
最小完整示例
在应用中调用 write_example(sys.stdout);示例测试使用 StringIO 并断言实际 JSON 输出与脱敏。
"""可执行的标准库日志接入,输出流由调用方管理。"""
import logging
from typing import TextIO
from stellarmesh_logging import JSONFormatter
def write_example(stream: TextIO) -> None:
"""将一条安全 JSON 写入 stream,不修改全局默认 Logger。"""
logger = logging.Logger("example", level=logging.INFO)
handler = logging.StreamHandler(stream)
try:
handler.setFormatter(JSONFormatter(static_fields={"service": "example-api"}))
logger.addHandler(handler)
logger.info("请求完成", extra={"password": "example-secret", "duration_ms": 12})
finally:
logger.removeHandler(handler)
handler.close()
# StreamHandler.close 不关闭调用方的 stream;文件或 StringIO 由外部 with 关闭。
关键限制
Formatter 不持有输出流、不发送 HTTP 或启动后台任务。JSON 用于结构化采集,PrettyFormatter 用于本地阅读;清洗不会扫描任意消息正文。
深入指南
Release files for stellarmesh-logging 0.5.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| stellarmesh_logging-0.5.1.tar.gz | 11.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| stellarmesh_logging-0.5.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 18.4 kB
Release files / stellarmesh_logging-0.5.1.tar.gz
| Download URL | stellarmesh_logging-0.5.1.tar.gz |
|---|---|
| Size | 11.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ed62dc57fa2b01c8da95c7538873c19cd91327f9106bf4697e69efe11366dda6
|
|
BLAKE2b-256 checksum How to use checksums |
6276194d507a11927e12cbd6cd16f1679407c06155be701e463f8f97a945b77d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 25, 2026.
Transparency logRelease files / stellarmesh_logging-0.5.1-py3-none-any.whl
| Download URL | stellarmesh_logging-0.5.1-py3-none-any.whl |
|---|---|
| Size | 7.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3b80b8c84703479aebbe99d1dc67f3e3f88597364fe39eb83cdfd97e8759c1ab
|
|
BLAKE2b-256 checksum How to use checksums |
c747eca17ed2cb1391076ef3ae1035c71ec96f2de3adffa8ec1bb2c92242b497
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 25, 2026.
Transparency log