Python logging模塊異步線程寫日志實(shí)現(xiàn)過程解析
通過logging模塊,重寫一個(gè)logging2模塊,獨(dú)立開啟線程,將待寫的日志信息異步放入隊(duì)列,做到日志輸出不影響主流程性能,環(huán)境python3.8
logging2.py
import osimport threadingimport queueimport timeimport datetimeimport loggingfrom logging.handlers import RotatingFileHandlerclass logging2(threading.Thread): AQueue = queue.Queue(100000) nPID = os.getpid() Adt = datetime.datetime.now().strftime(’%Y%m%d’) nCount = 1 def __init__(self, threadID, name, module, logLevel): threading.Thread.__init__(self) self.threadID = threadID self.name = name self.module = moduleprint('set loglevel: [%s]' % (logLevel) ) formatter = logging.Formatter(’%(asctime)s|%(name)s|%(process)d|%(levelname)s|%(message)s’) logfile = 'log_' + self.module + '_' + str(logging2.nPID) + '_' + str(logging2.Adt) + '.log' self.logger = logging.getLogger(__name__)self.rHandler = RotatingFileHandler(logfile, maxBytes = 10*1024*1024, backupCount = 10) self.rHandler.setFormatter(formatter)self.console = logging.StreamHandler() self.console.setFormatter(formatter)if logLevel == ’DEBUG’ : self.logger.setLevel(level = logging.DEBUG) self.rHandler.setLevel(logging.DEBUG) self.console.setLevel(logging.DEBUG) elif logLevel == ’INFO’ : self.logger.setLevel(level = logging.INFO) self.rHandler.setLevel(logging.INFO) self.console.setLevel(logging.INFO) elif logLevel == ’WARNING’ : self.logger.setLevel(level = logging.WARN) self.rHandler.setLevel(logging.WARN) self.console.setLevel(logging.WARN) elif logLevel == ’ERROR’ : self.logger.setLevel(level = logging.ERROR) self.rHandler.setLevel(logging.ERROR) self.console.setLevel(logging.ERROR)self.logger.addHandler(self.rHandler) self.logger.addHandler(self.console) #如果跨天了,則重新生成新的文件名 def reSetLog(self): AdtTemp = datetime.datetime.now().strftime(’%Y%m%d’) #比較新的時(shí)間 if AdtTemp == logging2.Adt: return(True) logging2.Adt = AdtTemp logfile = 'log_' + self.module + '_' + str(logging2.nPID) + '_' + str(AdtTemp) + '.log' self.rHandler = RotatingFileHandler(logfile, maxBytes = 1*1024, backupCount = 10)self.logger.addHandler(self.rHandler) self.logger.addHandler(self.console) logging2.nCount += 1 def run(self): print ('開啟日志線程:' + self.name) i = 0 while True: #data = 'queue test data' #debug(data) #print('Queuesize: %s' % (logging2.AQueue.qsize())) self.reSetLog() if logging2.AQueue.empty() == False:#從隊(duì)列獲取日志消息data = logging2.AQueue.get()#解析日志消息,格式:日志級(jí)別,內(nèi)容level = list(data.keys())[0]content = data.get(level)#把內(nèi)容按分隔符|解析成list傳入?yún)?shù)lstContent = list(content.split(’|’))if level == ’DEBUG’ : self.logger.debug(*lstContent)elif level == ’INFO’ : self.logger.info(*lstContent)elif level == ’WARNING’ : self.logger.warn(*lstContent)elif level == ’ERROR’ : self.logger.error(*lstContent) else:time.sleep(0.5) print ('退出線程:' + self.name) def debug(*content): logMsg = '' #傳入多個(gè)參數(shù)用豎線分隔符分開 for i in range(len(content)): if i == len(content)-1: logMsg += content[i] else: logMsg += content[i]+'|' logging2.AQueue.put({’DEBUG’:logMsg}) def info(*content): logMsg = '' for i in range(len(content)): if i == len(content)-1: logMsg += content[i] else: logMsg += content[i]+'|' logging2.AQueue.put({’INFO’:logMsg})def warn(*content): logMsg = '' for i in range(len(content)): if i == len(content)-1: logMsg += content[i] else: logMsg += content[i]+'|' logging2.AQueue.put({’WARNING’:logMsg}) def error(*content): logMsg = '' for i in range(len(content)): if i == len(content)-1: logMsg += content[i] else: logMsg += content[i]+'|' logging2.AQueue.put({’ERROR’:logMsg}) def init(module, level): # 創(chuàng)建新線程 thread1 = logging2(1, 'Thread-log', module, level) # 開啟新線程 thread1.start()# thread1.join()
測(cè)試樁logMain.py
import sysimport osimport timeimport threadingif __name__ == ’__main__’: import logging2 logging2.init('logMain', 'DEBUG') teststr = '22222' while True: logging2.debug(’this is a debug log test [%s] ’, teststr) logging2.info(’this is a info log test [%s] [%s]’, teststr, teststr) logging2.warn(’this is a warn log test’) logging2.error(’this is a error log test’) #time.sleep(0.1) print(threading.enumerate()) print(’press ctrl_c to exit’)
測(cè)試結(jié)果
生成日志文件:
-rw-rw-r--. 1 zxl zxl 10152463 6月 24 17:52 log_logMain_57554_20200624.log
文件內(nèi)容如下:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. PHP設(shè)計(jì)模式中工廠模式深入詳解2. JSP數(shù)據(jù)交互實(shí)現(xiàn)過程解析3. .NET中l(wèi)ambda表達(dá)式合并問題及解決方法4. 解決AJAX返回狀態(tài)200沒有調(diào)用success的問題5. ThinkPHP5實(shí)現(xiàn)JWT Token認(rèn)證的過程(親測(cè)可用)6. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說明7. 利用promise及參數(shù)解構(gòu)封裝ajax請(qǐng)求的方法8. CSS hack用法案例詳解9. Ajax實(shí)現(xiàn)表格中信息不刷新頁(yè)面進(jìn)行更新數(shù)據(jù)10. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向
