2017-12-21-multi-process-log

layouttitledescriptionkeywordscategorytags
post
多进程日志问题
多进程日志问题
python
python
python

2017-12-21-multi-process-log

  1. python 在多进程下打日志出现丢失错乱等问题,是由于 python 源码的问题。可以引入第三方包进行轻松解决
pip install ConcurrentLogHandler

sample.py

log = getLogger()
# Use an absolute path to prevent file rotation trouble.
logfile = os.path.abspath("mylogfile.log")
# Rotate log after reaching 512K, keep 5 old copies.
rotateHandler = ConcurrentRotatingFileHandler(logfile, "a", 512*1024, 5)
log.addHandler(rotateHandler)
log.setLevel(INFO)

if __name__ == '__main__':
    import time, os
    from logging import getLogger, INFO
    from cloghandler import ConcurrentRotatingFileHandler

    log = getLogger()
    # Use an absolute path to prevent file rotation trouble.
    logfile = os.path.abspath("mylogfile.log")
    # Rotate log after reaching 512K, keep 5 old copies.
    rotateHandler = ConcurrentRotatingFileHandler(logfile, "a", 512*1024, 5)
    log.addHandler(rotateHandler)
    log.setLevel(INFO)

    while True:
        log.info( str(os.getpid()) + " " + str(time.time()))
        time.sleep(1)

sample.config.py

if __name__ == '__main__':
    import os, time
    import logging, logging.config
    import cloghandler

    logging.config.fileConfig("logging.ini")
    log = logging.getLogger()

    while True:
        log.info(str(os.getpid()) + " " + str(time.time()))
        time.sleep(1)

logging.ini

[loggers]
keys=root

[handlers]
keys=hand01

[formatters]
keys=form01

[logger_root]
level=NOTSET
handlers=hand01

[handler_hand01]
class=handlers.ConcurrentRotatingFileHandler
level=NOTSET
formatter=form01
args=("rotating.log", "a", 512*1024, 5)

[formatter_form01]
format=%(asctime)s %(levelname)s %(message)s
1. nohup python xxx & 多个文件进行观察日志打印情况
2. ps aux | grep xxx | grep -v grep | awk '{print $2}' | xargs kill -9  # 杀死所有进程
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值