python多進(jìn)程下實(shí)現(xiàn)日志記錄按時(shí)間分割
python多進(jìn)程下實(shí)現(xiàn)日志記錄按時(shí)間分割,供大家參考,具體內(nèi)容如下
原理:自定義日志handler繼承TimedRotatingFileHandler,并重寫computeRollover與doRollover函數(shù)。其中重寫computeRollover是為了能按整分鐘/小時(shí)/天來(lái)分割日志,如按天分割,2018-04-10 00:00:00~2018-04-11 00:00:00,是一個(gè)半閉半開區(qū)間,且不是原意的:從日志創(chuàng)建時(shí)間或當(dāng)前時(shí)間開始,到明天的這個(gè)時(shí)候。
代碼如下:
#!/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):
# 將時(shí)間取整
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))
# 修改內(nèi)容--開始
# 在多進(jìn)程下,若發(fā)現(xiàn)dfn已經(jīng)存在,則表示已經(jīng)有其他進(jìn)程將日志文件按時(shí)間切割了,只需重新打開新的日志文件,寫入當(dāng)前日志;
# 若dfn不存在,則將當(dāng)前日志文件重命名,并打開新的日志文件
if not os.path.exists(dfn):
try:
self.rotate(self.baseFilename, dfn)
except FileNotFoundError:
# 這里會(huì)出異常:未找到日志文件,原因是其他進(jìn)程對(duì)該日志文件重命名了,忽略即可,當(dāng)前日志不會(huì)丟失
pass
# 修改內(nèi)容--結(jié)束
# 原內(nèi)容如下:
"""
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
說(shuō)明
第一次修改,如有不妥之處,還請(qǐng)指出,不勝感激。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
macOS M1(AppleSilicon) 安裝TensorFlow環(huán)境
蘋果為M1芯片的Mac提供了TensorFlow的支持,本文主要介紹了如何給使用M1芯片的macOS安裝TensorFlow的環(huán)境,感興趣的可以了解一下2021-08-08
Python輕松寫個(gè)課堂隨機(jī)點(diǎn)名系統(tǒng)
現(xiàn)在的學(xué)生大部分都很積極,會(huì)主動(dòng)舉手回答問(wèn)題。但是,也會(huì)遇到一些不好的情況,比如年級(jí)越高主動(dòng)舉手的人越少,所以本文寫了一個(gè)隨機(jī)的學(xué)生點(diǎn)名系統(tǒng)可以幫老師解決這些問(wèn)題2023-01-01
使用python調(diào)用zxing庫(kù)生成二維碼圖片詳解
本篇文章主要介紹了使用python調(diào)用zxing庫(kù)生成二維碼圖片,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01
OpenCV簡(jiǎn)單標(biāo)準(zhǔn)數(shù)字識(shí)別的完整實(shí)例
這篇文章主要給大家介紹了關(guān)于OpenCV簡(jiǎn)單標(biāo)準(zhǔn)數(shù)字識(shí)別的相關(guān)資料,要通過(guò)opencv 進(jìn)行數(shù)字識(shí)別離不開訓(xùn)練庫(kù)的支持,需要對(duì)目標(biāo)圖片進(jìn)行大量的訓(xùn)練,才能做到精準(zhǔn)的識(shí)別出目標(biāo)數(shù)字,需要的朋友可以參考下2021-09-09
利用pyecharts讀取csv并進(jìn)行數(shù)據(jù)統(tǒng)計(jì)可視化的實(shí)現(xiàn)
這篇文章主要介紹了利用pyecharts讀取csv并進(jìn)行數(shù)據(jù)統(tǒng)計(jì)可視化的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Python3實(shí)現(xiàn)計(jì)算兩個(gè)數(shù)組的交集算法示例
這篇文章主要介紹了Python3實(shí)現(xiàn)計(jì)算兩個(gè)數(shù)組的交集算法,結(jié)合2個(gè)實(shí)例形式總結(jié)分析了Python3針對(duì)數(shù)組的遍歷、位運(yùn)算以及元素的添加、刪除等相關(guān)操作技巧,需要的朋友可以參考下2019-04-04

