python多進(jìn)程下實(shí)現(xiàn)日志記錄按時(shí)間分割
python多進(jìn)程下實(shí)現(xiàn)日志記錄按時(shí)間分割,供大家參考,具體內(nèi)容如下
原理:自定義日志handler繼承TimedRotatingFileHandler,并重寫(xiě)computeRollover與doRollover函數(shù)。其中重寫(xiě)computeRollover是為了能按整分鐘/小時(shí)/天來(lái)分割日志,如按天分割,2018-04-10 00:00:00~2018-04-11 00:00:00,是一個(gè)半閉半開(kāi)區(qū)間,且不是原意的:從日志創(chuàng)建時(shí)間或當(dāng)前時(shí)間開(kāi)始,到明天的這個(gè)時(shí)候。
代碼如下:
#!/usr/bin/env python # encoding: utf-8 """自定義日志處理類(lèi)""" 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)容--開(kāi)始 # 在多進(jìn)程下,若發(fā)現(xiàn)dfn已經(jīng)存在,則表示已經(jīng)有其他進(jìn)程將日志文件按時(shí)間切割了,只需重新打開(kāi)新的日志文件,寫(xiě)入當(dāng)前日志; # 若dfn不存在,則將當(dāng)前日志文件重命名,并打開(kāi)新的日志文件 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í)有所幫助,也希望大家多多支持腳本之家。
- Python使用logging實(shí)現(xiàn)多進(jìn)程安全的日志模塊
- python?logging多進(jìn)程多線程輸出到同一個(gè)日志文件的實(shí)戰(zhàn)案例
- python 實(shí)現(xiàn)多進(jìn)程日志輪轉(zhuǎn)ConcurrentLogHandler
- python logging日志模塊以及多進(jìn)程日志詳解
- python中日志logging模塊的性能及多進(jìn)程詳解
- 詳解Python中l(wèi)ogging日志模塊在多進(jìn)程環(huán)境下的使用
- python多進(jìn)程日志以及分布式日志的實(shí)現(xiàn)方式
相關(guān)文章
Python中不同類(lèi)之間調(diào)用方法的四種方式小結(jié)
類(lèi)是一種面向?qū)ο蟮木幊谭妒?它允許我們將數(shù)據(jù)和功能封裝在一個(gè)實(shí)體中,本文主要介紹了Python中不同類(lèi)之間調(diào)用方法的四種方式小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02tesserocr與pytesseract模塊的使用方法解析
這篇文章主要介紹了tesserocr與pytesseract模塊的使用方法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08python調(diào)用kubernetesAPI簡(jiǎn)單使用方法
這篇文章主要介紹了python調(diào)用kubernetesAPI簡(jiǎn)單使用方法,K8s也提供API接口,提供這個(gè)接口的是管理節(jié)點(diǎn)的apiserver組件,下文更多相關(guān)內(nèi)容,需要的小伙伴可以參考一下2022-05-05Django數(shù)據(jù)庫(kù)類(lèi)庫(kù)MySQLdb使用詳解
Django項(xiàng)目要操作數(shù)據(jù)庫(kù),首先要和數(shù)據(jù)庫(kù)建立連接,才能讓程序中的數(shù)據(jù)和數(shù)據(jù)庫(kù)關(guān)聯(lián)起來(lái)進(jìn)行數(shù)據(jù)的增刪改查操作。這篇文章主要介紹了Django數(shù)據(jù)庫(kù)類(lèi)庫(kù)MySQLdb使用詳解,感興趣的小伙伴們可以參考一下2019-04-04Python?HMAC模塊維護(hù)數(shù)據(jù)安全技術(shù)實(shí)例探索
本篇博客將帶領(lǐng)讀者深入探索Python中HMAC模塊的高級(jí)應(yīng)用,通過(guò)豐富的示例代碼和詳細(xì)的解釋,揭示HMAC在實(shí)際應(yīng)用場(chǎng)景中的多面光芒,從基礎(chǔ)概念到密碼存儲(chǔ)、文件完整性驗(yàn)證、API安全,再到與加密算法的巧妙結(jié)合2024-01-01Python中chinesecalendar簡(jiǎn)介、安裝、使用方法詳細(xì)講解
這篇文章主要介紹了Python中chinesecalendar簡(jiǎn)介、安裝、使用方法詳細(xì)講解,該庫(kù)是判斷某年某月某一天是不是工作日/節(jié)假日。 支持 2004年 至 2023年,包括 2020年 的春節(jié)延長(zhǎng),需要的朋友可以參考下2023-03-03Python實(shí)現(xiàn)Canny及Hough算法代碼實(shí)例解析
這篇文章主要介紹了Python實(shí)現(xiàn)Canny與Hough算法代碼實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08基于Python實(shí)現(xiàn)簡(jiǎn)單的學(xué)生點(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)題2022-09-09