Skip to main content

A multiprocessing-safe logging system with Rich support.

Project description

logging-mp

🧰 一个支持多进程、带有 Rich 彩色输出的日志系统(multiprocessing-safe logging system with Rich support)

logging-mp 是一个轻量级的 Python 日志工具,解决了在多进程环境下日志错乱、丢失、顺序混乱等问题,并支持漂亮的终端输出(基于 rich)。


🚀 特性 Features

  • 多进程安全:使用 multiprocessing.QueueQueueHandler 实现
  • 独立日志监听进程:避免主进程阻塞
  • 彩色终端输出:集成 rich.logging.RichHandler
  • 支持动态日志等级设置:兼容 logging.DEBUG/INFO/WARNING/...
  • 简单易用:无需复杂配置,一行代码启动

📦 安装 Installation

pip install logging-mp

或克隆本仓库:

git clone https://github.com/silencht/logging-mp.git
cd logging-mp
pip install .

🛠️ 使用方法 Usage

from multiprocessing import Process
from logging_mp import logging_mp

def worker(name, level=None):
    # 如果没有设置 level,就使用全局默认等级
    logger = logging_mp.get_logger(name, level=level)

    logger.debug(f"[{name}] debug —— 调试细节")
    logger.info(f"[{name}] info —— 普通信息")
    logger.warning(f"[{name}] warning —— 警告但可以运行")
    logger.error(f"[{name}] error —— 出错但还能撑住")
    logger.critical(f"[{name}] critical —— 严重错误可能导致崩溃")

if __name__ == "__main__":
    # 全局默认等级:INFO(即不显示 debug)
    logging_mp.basic_config(level=logging_mp.INFO)

    main_logger = logging_mp.get_logger("main")
    main_logger.info("主进程启动")

    # 启动多个子进程:
    # 👉 worker-A:不设置等级,使用全局 INFO
    # 👉 worker-B:设置为 DEBUG,打印全部
    # 👉 worker-C:设置为 WARNING,只显示 warning 及以上
    # 👉 worker-D:不设置等级,也服从全局 INFO
    processes = [
        Process(target=worker, args=("worker-A",)),                        # 使用全局等级 INFO
        Process(target=worker, args=("worker-B", logging_mp.DEBUG)),       # 单独设置为 DEBUG
        Process(target=worker, args=("worker-C", logging_mp.WARNING)),     # 单独设置为 WARNING
        Process(target=worker, args=("worker-D",)),                        # 使用全局等级 INFO
    ]

    for p in processes:
        p.start()
    for p in processes:
        p.join()

    main_logger.info("主进程结束")

📄 License

本项目采用 MIT License。

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

logging_mp-0.1.3.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

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

logging_mp-0.1.3-py3-none-any.whl (3.4 kB view details)

Uploaded Python 3

File details

Details for the file logging_mp-0.1.3.tar.gz.

File metadata

  • Download URL: logging_mp-0.1.3.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.19

File hashes

Hashes for logging_mp-0.1.3.tar.gz
Algorithm Hash digest
SHA256 e5a889b80967aa22427de2f8b5e964cb0ed34458505fcfc2039e85ea3b052136
MD5 5b4cb4c70dfbe662289f776c0c47778c
BLAKE2b-256 ea26552d15a01d70afc247df33dc1123ab809b139b5b0c8ebad74f4e7d1db5bc

See more details on using hashes here.

File details

Details for the file logging_mp-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: logging_mp-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 3.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.19

File hashes

Hashes for logging_mp-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 0bcddd467e0ab423d8d2fee6ce56cbe63d6a7e0630299b7931c40cfe1539ddbc
MD5 66d3aeb433697c20d6ab801eb89a277a
BLAKE2b-256 f8a216c4567aa9efcc88f832ad0f8fbd650d5d6b5c51cfd8053c32c8867e5a5e

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