Skip to main content

simple-channel-log

Release Python Version License Downloads

轻量高效的日志库,支持多级别日志记录、日志轮转、流水日志追踪及埋点日志功能,深度集成 Flask,FastAPI,Requests,Unirest 以及 CTEC-Consumer 框架。

主要特性

  • 📅 支持按时间轮转日志(天/小时/分钟等)
  • 📊 提供多级别日志记录(DEBUG/INFO/WARNING/ERROR/CRITICAL)
  • 🌐 内置 Flask,FastAPI 中间件记录请求/响应流水日志
  • 📡 集成 Requests,Unirest 会话记录外部调用流水日志
  • 📡 集成 CTEC-Consumer 记录消费者流水日志
  • 🔍 智能处理长字符串截断(超过1000字符自动标记)
  • 📁 自动创建分级日志目录结构
  • 💻 支持终端输出与文件存储分离控制

安装

pip install simple_channel_log

快速入门

基础配置

# coding:utf-8
import simple_channel_log as log

# 初始化日志配置
log.__init__("<your_appname>", logdir="/app/logs")

# 初始化后可直接调用日志方法,日志将记录到参数 `logdir` 指定的目录中
log.debug("调试信息", extra_field="value")
log.info("业务日志", user_id=123)
log.warning("异常预警", error_code=500)
log.error("系统错误", stack_trace="...")

# 埋点日志
log.trace(
    user_id=123,
    action="purchase",
    item_count=2
)

Flask 流水日志

导入 Flask 库并初始化 simple_channel_log 即自动启用 Flask 流水日志,将自动记录每个接口的调用信息。

# coding:utf-8
import simple_channel_log as log
from flask import Flask

app = Flask(__name__)


@app.get("/index")
# 若要在日志中记录接口编码,你需要使用如下装饰器显式设置
# @log.method_code("I00101")
def index():
    return {"msg": "ok"}


if __name__ == "__main__":
    # 初始化日志配置
    log.__init__("<your_appname>")

    # 启动后访问接口将自动记录流水日志
    app.run()

FastAPI 流水日志

导入 FastAPI 库并初始化 simple_channel_log 即自动启用 FastAPI 流水日志。

import uvicorn
import simple_channel_log as log
from fastapi import FastAPI, Request

app = FastAPI()

# 初始化日志配置
log.__init__("<your_appname>")


@app.get("/index")
# 若要在日志中记录接口编码,你需要使用如下装饰器显式设置
# @log.method_code("I00101")
async def index(request: Request):
    return {"msg": "ok"}


if __name__ == "__main__":
    # 启动后访问接口将自动记录流水日志
    uvicorn.run("main:app")

Requests 外部调用追踪

导入 requests 库并初始化 simple_channel_log 即自动启用 requests 外部调用追踪,将自动记录每个请求的调用信息。

# coding:utf-8
import requests
import simple_channel_log

# 初始化日志配置
simple_channel_log.__init__("<your_appname>")

# 发起请求后将自动记录外部流水日志
r = requests.get("http://gqylpy.com/index")

Unirest 外部调用追踪

导入 unirest 库并初始化 simple_channel_log 即自动启用 unirest 外部调用追踪。

# coding:utf-8
import unirest
import simple_channel_log

# 初始化日志配置
simple_channel_log.__init__("<your_appname>")

# 发起请求后将自动记录外部流水日志
r = unirest.get("http://gqylpy.com/index")

详细配置

初始化参数

参数名 类型 默认值 说明
appname str 必填 应用名称,以服务编码开头(小写),以下划线拼接
logdir str 系统相关默认路径 日志存储根目录
when str 'D' 轮转周期:W(周)/D(天)/H(时)/M(分)/S(秒)
interval int 1 轮转频率
backup_count int 7 历史日志保留数量(0=永久)
output_to_terminal bool False 启用后日志将同时输出到控制台

Metadata

Release files for simple-channel-log 1.8.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for simple-channel-log 1.8.6
File Size Uploaded Provenance Actions
simple_channel_log-1.8.6.tar.gz 15.6 kB Without provenance Details for simple_channel_log-1.8.6.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for simple-channel-log 1.8.6
File Interpreter ABI Platform Actions
simple_channel_log-1.8.6-py3-none-any.whl Python 3 none any Details for simple_channel_log-1.8.6-py3-none-any.whl

Total release size: 30.4 kB

Release files / simple_channel_log-1.8.6.tar.gz

Download URL simple_channel_log-1.8.6.tar.gz
Size 15.6 kB
Tags Source
SHA-256 checksum
How to use checksums
9207e0b875816fd360910754c4e0030bea39d38461d099e4779ca7d78303705c
BLAKE2b-256 checksum
How to use checksums
9f8803862b81ec0704728d217bc86293246818c0339205368a0cd5f679bef66a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release files / simple_channel_log-1.8.6-py3-none-any.whl

Download URL simple_channel_log-1.8.6-py3-none-any.whl
Size 14.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
28bdebe944e9767322b6728890b7f9c8796dc3a1f3a61ee13ed49a44e2cde4a9
BLAKE2b-256 checksum
How to use checksums
c25975e364dad5ac62a26e44cc05810c1439108ff9ff0d653a502d227699ab2a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Security for simple-channel-log 1.8.6

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.

Release history Release notifications | RSS feed

This release

1.8.6 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page