Skip to main content

MultProcTimedRotatingFileHandler

支持按时间滚动的Python多进程日志Handler, 支支持macOS/Linux, 自用未充分测试

参考了 https://github.com/yorks/mpfhandler, 有如下优化

  • 添加了intervel支持
  • 支持备份自动清理
  • 使用MMAP优化了性能

性能基准

8核CPU, 8进程, 100000条长度47字符日志测试, 相比TimedRotatingFileHandler大约有25%-30%性能损耗

随着进程数增大, 锁竞争也变激烈了, 性能损耗也会进一步增大, 100进程下会有50%-60%性能损耗

8进程, QPS情况

  • MultProcTimedRotatingFileHandler: 23482
  • TimedRotatingFileHandler: 30349

配置示例

import logging.config

LOGGING_CONFIG = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '[%(levelname)s %(asctime)s %(name)s %(lineno)d %(process)d %(thread)d] %(message)s'
        },
    },
    'handlers': {
        'console': {
            'level': 'INFO',
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
        'backend_file':{
            'class': 'mptrfhandler.MultProcTimedRotatingFileHandler',
            'filename': 'main.log',
            'when': 'H',  # 按小时滚动
            'backupCount': 10,                                   #备份份数
            'formatter':'verbose',                              #使用哪种formatters日志格式
            'level': 'DEBUG',
            'delay': True,
        },
    },
    'loggers': {
        '': {
            'handlers': ['backend_file'],
            'level': "INFO",
            'propagate': False,
        },
    },
}

logging.config.dictConfig(LOGGING_CONFIG)

Release files for mptrfhandler 0.0.8

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

Source distribution (sdist)

Source distribution for mptrfhandler 0.0.8
File Size Uploaded
mptrfhandler-0.0.8.tar.gz 8.1 kB Details

Release files / mptrfhandler-0.0.8.tar.gz

Download URL mptrfhandler-0.0.8.tar.gz
Size 8.1 kB
Tags Source
SHA-256 checksum
How to use checksums
91af9cdf90e5b9ac5fb6bdad817bed7b5590f0bff404a220be796a4b47672b4f
BLAKE2b-256 checksum
How to use checksums
dad9ad2b154aaa3e8ab0e00a05b035cc5e028c665a39117eb8324078d5b02b08
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

Release history Release notifications | RSS feed

This release

0.0.8 This release

1 release file

0.0.7

1 release file

0.0.6

1 release file

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