美文网首页
使用mlogging实现多进程时间分割日志记录

使用mlogging实现多进程时间分割日志记录

作者: 楠木cral | 来源:发表于2020-04-27 11:04 被阅读0次

最近公司让实现项目的日志文件如标题所说的需求,于是在网上找了开源包,mlogging.Github上https://github.com/kieslee/mlogging
一. 这里主要是利用它重写TimedRotatingFileHandler的TimedRotatingFileHandler_MP:按照时间自动分割日志文件,实现它的多进程应用

image.png
关键代码是这段
fcntl.flock(f.fileno(), fcntl.LOCK_EX)
FileHandler_MP.emit(self, record)
fcntl.flock(f.fileno(), fcntl.LOCK_UN)
f.close()

fcntl.flock(f.fileno(), fcntl.LOCK_EX) 意思是给文件加锁
fcntl.flock(f.fileno(), fcntl.LOCK_UN)意思是给文件解锁.
二. 另外fcntl windows下载不到,她是用于linux下的用于文件锁的模块。如果是在windows上测试,需要将

except IOError, e:
    pass
 
================ 修改为
 
except :
    pass

三. 配置
在 loggerManager loggerManager里面将TimedRotatingFileHandler的地方更换成TimedRotatingFileHandler_MP,在setting.py里面的LOGGING的handlers中的文件日志更换:

'file': {
            'level': 'INFO',
            'class': 'mlogging.TimedRotatingFileHandler_MP',
            'formatter': 'verbose',
            'filename': webservice_logfile2,
            'when': 'midnight'
        },

基本上就可以了

相关文章

网友评论

      本文标题:使用mlogging实现多进程时间分割日志记录

      本文链接:https://www.haomeiwen.com/subject/wdoewhtx.html