python logging 重復(fù)寫日志問題解決辦法詳解
python logging 重復(fù)寫日志問題
用Python的logging模塊記錄日志時(shí),遇到了重復(fù)記錄日志的問題,第一條記錄寫一次,第二條記錄寫兩次,第三條記錄寫三次。。。很頭疼,這樣記日志可不行。網(wǎng)上搜索到了原因與解決方案:
原因:沒有移除handler
解決:在日志記錄完之后removeHandler
修改前示例代碼:
import logging
def log(message):
logger = logging.getLogger('testlog')
streamhandler = logging.StreamHandler()
streamhandler.setLevel(logging.ERROR)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(name)s - %(message)s')
streamhandler.setFormatter(formatter)
logger.addHandler(streamhandler)
logger.error(message)
if __name__ == '__main__':
log('hi')
log('hi too')
log('hi three')
修改前輸出結(jié)果:
2016-07-08 09:17:29,740 - ERROR - testlog - hi
2016-07-08 09:17:29,740 - ERROR - testlog - hi too
2016-07-08 09:17:29,740 - ERROR - testlog - hi too
2016-07-08 09:17:29,740 - ERROR - testlog - hi three
2016-07-08 09:17:29,740 - ERROR - testlog - hi three
2016-07-08 09:17:29,740 - ERROR - testlog - hi three
修改后示例代碼:
import logging
def log(message):
logger = logging.getLogger('testlog')
streamhandler = logging.StreamHandler()
streamhandler.setLevel(logging.ERROR)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(name)s - %(message)s')
streamhandler.setFormatter(formatter)
logger.addHandler(streamhandler)
logger.error(message)
# 添加下面一句,在記錄日志之后移除句柄
logger.removeHandler(streamhandler)
if __name__ == '__main__':
log('hi')
log('hi too')
log('hi three')
修改后輸出結(jié)果:
2016-07-08 09:32:28,206 - ERROR - testlog - hi
2016-07-08 09:32:28,206 - ERROR - testlog - hi too
2016-07-08 09:32:28,206 - ERROR - testlog - hi three
深度解析:
Google之后,大概搞明白了,就是你第二次調(diào)用log的時(shí)候,根據(jù)getLogger(name)里的name獲取同一個(gè)logger,而這個(gè)logger里已經(jīng)有了第一次你添加的handler,第二次調(diào)用又添加了一個(gè)handler,所以,這個(gè)logger里有了兩個(gè)同樣的handler,以此類推,調(diào)用幾次就會有幾個(gè)handler。。
所以這里有以下幾個(gè)解決辦法:
- 每次創(chuàng)建不同name的logger,每次都是新logger,不會有添加多個(gè)handler的問題。(ps:這個(gè)辦法太笨,不過我之前就是這么干的。。)
- 像上面一樣每次記錄完日志之后,調(diào)用removeHandler()把這個(gè)logger里的handler移除掉。在log方法里做判斷,如果這個(gè)logger已有handler,則不再添加handler。
- 與方法2一樣,不過把用pop把logger的handler列表中的handler移除。
下面是方法3與方法4的代碼示例:
方法3:
import logging
def log(message):
logger = logging.getLogger('testlog')
# 這里進(jìn)行判斷,如果logger.handlers列表為空,則添加,否則,直接去寫日志
if not logger.handlers:
streamhandler = logging.StreamHandler()
streamhandler.setLevel(logging.ERROR)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(name)s - %(message)s')
streamhandler.setFormatter(formatter)
logger.addHandler(streamhandler)
logger.error(message)
if __name__ == '__main__':
log('hi')
log('hi too')
log('hi three')
方法4:
import logging
def log(message):
logger = logging.getLogger('testlog')
streamhandler = logging.StreamHandler()
streamhandler.setLevel(logging.ERROR)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(name)s - %(message)s')
streamhandler.setFormatter(formatter)
logger.addHandler(streamhandler)
logger.error(message)
# 用pop方法把logger.handlers列表中的handler移除,注意如果你add了多個(gè)handler,這里需多次pop,或者可以直接為handlers列表賦空值
logger.handlers.pop()
# logger.handler = []
if __name__ == '__main__':
log('hi')
log('hi too')
log('hi three')
這幾種方法都親試可行,個(gè)人覺得方法3判斷更加優(yōu)雅,你覺得呢?
到此這篇關(guān)于python logging 重復(fù)寫日志問題j解決辦法詳解的文章就介紹到這了,更多相關(guān)python logging 重復(fù)寫日志問題內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Flask實(shí)現(xiàn)文件上傳七牛云中并下載
文件上傳是Web應(yīng)用中常見的功能之一,而七牛云則提供了強(qiáng)大的云存儲服務(wù),本文我們將學(xué)習(xí)如何在Flask應(yīng)用中實(shí)現(xiàn)文件上傳,并將上傳的文件保存到七牛云,感興趣的可以學(xué)習(xí)一下2023-10-10
Python3爬蟲里關(guān)于Splash負(fù)載均衡配置詳解
在本篇文章里小編給大家分享了關(guān)于Python3爬蟲里關(guān)于Splash負(fù)載均衡配置的相關(guān)內(nèi)容,需要的朋友們可以學(xué)習(xí)參考下。2020-07-07
Python 使用Opencv實(shí)現(xiàn)目標(biāo)檢測與識別的示例代碼
這篇文章主要介紹了Python 使用Opencv實(shí)現(xiàn)目標(biāo)檢測與識別的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
PyChar學(xué)習(xí)教程之自定義文件與代碼模板詳解
pycharm默認(rèn)的【新建】文件,格式很不友好,那么就需要改一下文件模板。下面這篇文章主要給大家介紹了關(guān)于PyChar學(xué)習(xí)教程之自定義文件與代碼模板的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面跟著小編來一起看看吧。2017-07-07
Django解決無法從request.POST中獲取URL傳進(jìn)來的參數(shù)
這篇文章主要介紹了Django解決無法從request.POST中獲取URL傳進(jìn)來的參數(shù)問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
Python在cmd上打印彩色文字實(shí)現(xiàn)過程詳解
這篇文章主要介紹了Python在cmd上打印彩色文字實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08

