您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關python多進程下中如何實現日志記錄按時間分割,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
python多進程下實現日志記錄按時間分割,供大家參考,具體內容如下
原理:自定義日志handler繼承TimedRotatingFileHandler,并重寫computeRollover與doRollover函數。其中重寫computeRollover是為了能按整分鐘/小時/天來分割日志,如按天分割,2018-04-10 00:00:00~2018-04-11 00:00:00,是一個半閉半開區間,且不是原意的:從日志創建時間或當前時間開始,到明天的這個時候。
代碼如下:
#!/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)) # 修改內容--開始 # 在多進程下,若發現dfn已經存在,則表示已經有其他進程將日志文件按時間切割了,只需重新打開新的日志文件,寫入當前日志; # 若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
說明
第一次修改,如有不妥之處,還請指出,不勝感激。
關于“python多進程下中如何實現日志記錄按時間分割”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。