ConcurrentLogHandler 在Windows下的问题解决

ConcurrentLogHandler 在Windows下的问题解决

问题描述

Python中的logging模块自带的一些handlers无法在多进程环境下使用,于是下载了Lowell Alleman's ConcurrentLogHandler的这个package。windows下安装时可能还需要安装pypiwin32pywin32不能用pip直接安装,而且pyenv下安装比较麻烦,所以推荐前者)。使用时出现了进程卡住的情况,查证之后发现是windows下的lock无法正常的获取和释放。

解决方法

  1. 进行环境判断
    在Linux环境下可以正常使用,但是windows下不行,所以可考虑将程序部署到Linux运行,本地windows测试时考虑使用非进程安全的handler调试。

  2. concurrent-log-handler
    Github上有位老哥修复了这个问题,并做成了可用的package,地址在这。安装完成后重新导入即可。
    下面贴下我的简单配置

LOGGING_CONFIG = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'verbose': {
            'format': "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
            'datefmt': "%Y-%m-%d %H:%M:%S"
        },
        'simple': {
            'format': '%(message)s'
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose'
        },
        'file': {
            'level': 'INFO',
            'class': 'concurrent_log_handler.ConcurrentRotatingFileHandler', #这里是重点
            'maxBytes': 1024 * 1024 * 10,
            'backupCount': 50,
            'delay': True,
            'filename': 'logs/logging.log',
            'formatter': 'simple'
        }
    },
    'loggers': {
        '': {
            'handlers': ['console', 'file'],
            'level': 'INFO',
        },
    }
}

使用方法

import os
from simple_settings import settings # 安利下这个配置管理库,挺好用的

os.environ['SIMPLE_SETTINGS'] = 'settings.development' # 把上面的配置存到development里面去
settings.setup()

logging.config.dictConfig(settings.LOGGING_CONFIG)
logger = logging.getLogger()

def test():
    logger.info("this is a test")

if __name__ == '__main__':
    test()

问题解决

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Collecting ConcurrentLogHandler==0.9.1 Using cached ConcurrentLogHandler-0.9.1.tar.gz (25 kB) Preparing metadata (setup.py): started Preparing metadata (setup.py): finished with status 'error' ERROR: Command errored out with exit status 1: command: 'D:\personal_center_p3\Scripts\python.exe' -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\V_YXYU~1\\AppData\\Local\\Temp\\pip-install-4qgsez5s\\concurrentloghandler_f371d83918a641ad82c3472b90ec9acc\\setup.py'"'"'; __file__='"'"'C:\\Users\\V_YXYU~1\\AppData\\Local\\Temp\\pip-install-4qgsez5s\\concurrentloghandler_f371d83918a641ad82c3472b90ec9acc\\setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base 'C:\Users\V_YXYU~1\AppData\Local\Temp\pip-pip-egg-info-ofm5zj3l' cwd: C:\Users\V_YXYU~1\AppData\Local\Temp\pip-install-4qgsez5s\concurrentloghandler_f371d83918a641ad82c3472b90ec9acc\ Complete output (1 lines): error in ConcurrentLogHandler setup command: use_2to3 is invalid. ---------------------------------------- WARNING: Discarding https://files.pythonhosted.org/packages/fd/e5/0dc4f256bcc6484d454006b02f33263b20f762a433741b29d53875e0d763/ConcurrentLogHandler-0.9.1.tar.gz#sha256=8225a590fd4194c413fa26675bde5f6b80ad79e4182d5876ba3e264f77755918 (from https://pypi.org/simple/concurrentloghandler/). Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output. ERROR: Could not find a version that satisfies the requirement ConcurrentLogHandler==0.9.1 (from versions: 0.7.2, 0.7.3, 0.7.4, 0.8.0, 0.8.1, 0.8.2, 0.8.3, 0.8.4, 0.8.5, 0.8.6, 0.8.7, 0.9.0, 0.9.1) ERROR: No matching distribution found for ConcurrentLogHandler==0.9.1
最新发布
07-25
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值