python多進程下實現(xiàn)日志記錄按時間分割
python多進程下實現(xiàn)日志記錄按時間分割,供大家參考,具體內容如下
原理:自定義日志handler繼承TimedRotatingFileHandler,并重寫computeRollover與doRollover函數(shù)。其中重寫computeRollover是為了能按整分鐘/小時/天來分割日志,如按天分割,2018-04-10 00:00:00~2018-04-11 00:00:00,是一個半閉半開區(qū)間,且不是原意的:從日志創(chuàng)建時間或當前時間開始,到明天的這個時候。
代碼如下:
#!/usr/bin/env python
# encoding: utf-8
"""自定義日志處理類"""
import os
import time
from logging.handlers import TimedRotatingFileHandler
class MyLoggingHandler(TimedRotatingFileHandler):
def __init__(self, filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False, atTime=None):
TimedRotatingFileHandler.__init__(self, filename, when=when, interval=interval, backupCount=backupCount, encoding=encoding, delay=delay, utc=utc, atTime=atTime)
def computeRollover(self, currentTime):
# 將時間取整
t_str = time.strftime(self.suffix, time.localtime(currentTime))
t = time.mktime(time.strptime(t_str, self.suffix))
return TimedRotatingFileHandler.computeRollover(self, t)
def doRollover(self):
"""
do a rollover; in this case, a date/time stamp is appended to the filename
when the rollover happens. However, you want the file to be named for the
start of the interval, not the current time. If there is a backup count,
then we have to get a list of matching filenames, sort them and remove
the one with the oldest suffix.
"""
if self.stream:
self.stream.close()
self.stream = None
# get the time that this sequence started at and make it a TimeTuple
currentTime = int(time.time())
dstNow = time.localtime(currentTime)[-1]
t = self.rolloverAt - self.interval
if self.utc:
timeTuple = time.gmtime(t)
else:
timeTuple = time.localtime(t)
dstThen = timeTuple[-1]
if dstNow != dstThen:
if dstNow:
addend = 3600
else:
addend = -3600
timeTuple = time.localtime(t + addend)
dfn = self.rotation_filename(self.baseFilename + "." +
time.strftime(self.suffix, timeTuple))
# 修改內容--開始
# 在多進程下,若發(fā)現(xiàn)dfn已經(jīng)存在,則表示已經(jīng)有其他進程將日志文件按時間切割了,只需重新打開新的日志文件,寫入當前日志;
# 若dfn不存在,則將當前日志文件重命名,并打開新的日志文件
if not os.path.exists(dfn):
try:
self.rotate(self.baseFilename, dfn)
except FileNotFoundError:
# 這里會出異常:未找到日志文件,原因是其他進程對該日志文件重命名了,忽略即可,當前日志不會丟失
pass
# 修改內容--結束
# 原內容如下:
"""
if os.path.exists(dfn):
os.remove(dfn)
self.rotate(self.baseFilename, dfn)
"""
if self.backupCount > 0:
for s in self.getFilesToDelete():
os.remove(s)
if not self.delay:
self.stream = self._open()
newRolloverAt = self.computeRollover(currentTime)
while newRolloverAt <= currentTime:
newRolloverAt = newRolloverAt + self.interval
# If DST changes and midnight or weekly rollover, adjust for this.
if (self.when == 'MIDNIGHT' or self.when.startswith('W')) and not self.utc:
dstAtRollover = time.localtime(newRolloverAt)[-1]
if dstNow != dstAtRollover:
if not dstNow: # DST kicks in before next rollover, so we need to deduct an hour
addend = -3600
else: # DST bows out before next rollover, so we need to add an hour
addend = 3600
newRolloverAt += addend
self.rolloverAt = newRolloverAt
說明
第一次修改,如有不妥之處,還請指出,不勝感激。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
macOS M1(AppleSilicon) 安裝TensorFlow環(huán)境
蘋果為M1芯片的Mac提供了TensorFlow的支持,本文主要介紹了如何給使用M1芯片的macOS安裝TensorFlow的環(huán)境,感興趣的可以了解一下2021-08-08
利用pyecharts讀取csv并進行數(shù)據(jù)統(tǒng)計可視化的實現(xiàn)
這篇文章主要介紹了利用pyecharts讀取csv并進行數(shù)據(jù)統(tǒng)計可視化的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-04-04
Python3實現(xiàn)計算兩個數(shù)組的交集算法示例
這篇文章主要介紹了Python3實現(xiàn)計算兩個數(shù)組的交集算法,結合2個實例形式總結分析了Python3針對數(shù)組的遍歷、位運算以及元素的添加、刪除等相關操作技巧,需要的朋友可以參考下2019-04-04

