Skip to main content

简介

loguru 基础上封装的 开箱即用的 python 日志 工具包
loguru https://loguru.readthedocs.io/en/stable/overview.html#installation
notifiers https://notifiers.readthedocs.io/en/latest/usage.html#provider-schema

目前为 开发版本,未经过严格测试,请勿使用在生产环境。

使用方法

pip install mlogs
from mlogs import MLogger

L = MLogger()
L.info("nice")

# trace_id 可以用来 链路追踪, topic 用于分类
L.warning("nice", trace_id="12312321", topic="model:task")
L.warning({"msg": "nice", "gg_monkey": "bugs"}, trace_id="12312321", topic="model:task")

# MLogger 会同时输出日志到 terminal 日志文件
# FileLogger   仅输出日志到 日志文件,按照日志级别生成 不同的日志文件
# StdoutLogger 仅输出日志到 terminal
# AdaptHistoryLogger 为了兼容历史项目日志


# 配置邮件接收 (release 0.1.3)
"""
email 发送邮件,使用的是python 原生库 smtplib.SMTP
"""
from mlogs import StdoutLogger

alerts = {
    "alerts_type": "email",
    "params": {
        "username": "username",
        "password": "password",
        "host": "smtp.exmail.qq.com",  # 邮箱服务器地址
        "port": 465,  # 邮箱服务器端口
        "from": "yourname@cc.com",  # 邮件发送人
        "to": [
            "yourname@cc.com",  # 邮件接收人
        ],
        "login": True,
        "ssl": True,  # 绝大多数邮箱,均开启了ssl服务,所以需要配置
    },
}

L = StdoutLogger(alerts=alerts)
L.error('nice')

# 添加 webhook,此时 会同时发送 邮件和 webhook
alerts = [
    {
        "alerts_type": "email",
        "params": {
            "username": "username",
            "password": "password",
            "host": "smtp.exmail.qq.com",  # 邮箱服务器地址
            "port": 465,  # 邮箱服务器端口
            "from": "yourname@cc.com",  # 邮件发送人
            "to": [
                "yourname@cc.com",  # 邮件接收人
            ],
            "login": True,
            "ssl": True,  # 绝大多数邮箱,均开启了ssl服务,所以需要配置
        },
    },
    {
        {
            "alerts_type": "feishu",
            "params": {
                "webhook_url": "https://open.feishu.cn/open-apis/bot/v2/hook/your_id",
                'msg_type': 'text',
                'content': {
                    'title': "from mlogs feishu",
                    'text': "this is log feishu test"
                }
            }
        }
    }
]

L = StdoutLogger(alerts=alerts)
L.error('nice! this is a test')

# default topic (release 0.2.0)
"""
当一个项目里面包含多个服务时,需要使用topic来区分,那么可以在日志初始化时,统一设置default topic。
"""
from mlogs import StdoutLogger

L1 = StdoutLogger(default_topic="1")
L2 = StdoutLogger(default_topic="2")
L3 = StdoutLogger(default_topic="3")

L1.info('nice')
L2.info('nice')
L3.info('nice', topic="333")

一些介绍

  1. FileLogger 默认按照 50 MB分割文件,最多储存 10 个文件,默认不压缩日志。
  2. 日志级别默认为DEBUG,可以自定义设置,也可以使用环境变量 export DEPLOYMENT=PRODUCTION,设置日志级别为 INFO

遗留问题

  1. 暂时使用自己修改过的 notifiers,支持飞书webhook。如果有幸 通过原始库合并,则会转到 原始库
  2. 如果 原本环境中 包含 notifiers,需要先卸载,否则会出现找不到 feishu。
  3. 原本的 notifiers 库,可以做一个钩子函数,支持添加外部 组件,这样就能很快捷的添加适合的组件了。

注意⚠️

  1. 打包时,会把 tests 文件夹下内容拷贝到资源包。所以包含敏感信息的测试文件,请放入 secret_tests 下。
  2. 目前测试范仅仅 覆盖 mac ubuntu

版本更新内容

0.3.5 新增 AdaptHistoryLogger 用来支持历史日志格式。
0.3.4 移除 默认设置 日志导出 支持 numpy 格式数据,现在是可选,通过 json_encoder参数 支持自定义json 解析类 传入。
0.3.3 修复 移除 默认设置 opt(color=True),防止输入的 msg(不可预料)内出现无法识别的 color tag 导致日志报错。 
0.3.2 新增 部分功能测试
0.3.0 新增 飞书 webhook 支持
0.2.x 修复 部分已知问题
0.2.0 新增 default topic,在类初始化时设置,则后续所有日志会添加此 topic  
0.1.3 新增 邮件发送配置  
0.0.10 工程能够使用  

Metadata

Release files for mlogs 0.3.5

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

Source distribution (sdist)

Source distribution for mlogs 0.3.5
File Size Uploaded
mlogs-0.3.5.tar.gz 38.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mlogs 0.3.5
File Interpreter ABI Platform
mlogs-0.3.5-py3-none-any.whl Python 3 none any Details

Total release size: 140.7 kB

Release files / mlogs-0.3.5.tar.gz

Download URL mlogs-0.3.5.tar.gz
Size 38.4 kB
Tags Source
SHA-256 checksum
How to use checksums
628585386079c84ec88dd68b8a9012911ad48ce36a65958e419188d3df4f84c4
BLAKE2b-256 checksum
How to use checksums
9b481de2ccfdb14f8dc697025653462b3c642a17c39978aed61adc34d0da53b0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.9

Release files / mlogs-0.3.5-py3-none-any.whl

Download URL mlogs-0.3.5-py3-none-any.whl
Size 102.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
43c5b8ce5616842444311acab2252f773339a2a64d830fca36b24daa0abb337e
BLAKE2b-256 checksum
How to use checksums
3b1c26e8414079bde791cd6d40c56aad649dddbe6819627f1a4024525ad1eff9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.9

Release history Release notifications | RSS feed

This release

0.3.5 This release

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.2

2 release files

0.2.0

2 release files

0.1.3

2 release files

0.0.9

2 release files

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