python logging 重復(fù)寫日志問題解決辦法詳解
python logging 重復(fù)寫日志問題
用Python的logging模塊記錄日志時(shí),遇到了重復(fù)記錄日志的問題,第一條記錄寫一次,第二條記錄寫兩次,第三條記錄寫三次。。。很頭疼,這樣記日志可不行。網(wǎng)上搜索到了原因與解決方案:
原因:沒有移除handler 解決:在日志記錄完之后removeHandler
修改前示例代碼:
import loggingdef 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 loggingdef 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)用幾次就會(huì)有幾個(gè)handler。。
所以這里有以下幾個(gè)解決辦法:
每次創(chuàng)建不同name的logger,每次都是新logger,不會(huì)有添加多個(gè)handler的問題。(ps:這個(gè)辦法太笨,不過我之前就是這么干的。。) 像上面一樣每次記錄完日志之后,調(diào)用removeHandler()把這個(gè)logger里的handler移除掉。在log方法里做判斷,如果這個(gè)logger已有handler,則不再添加handler。 與方法2一樣,不過把用pop把logger的handler列表中的handler移除。下面是方法3與方法4的代碼示例:
方法3:
import loggingdef 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 loggingdef 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)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. XML在語(yǔ)音合成中的應(yīng)用2. HTTP協(xié)議常用的請(qǐng)求頭和響應(yīng)頭響應(yīng)詳解說明(學(xué)習(xí))3. 不要在HTML中濫用div4. ASP將數(shù)字轉(zhuǎn)中文數(shù)字(大寫金額)的函數(shù)5. .NET Framework各版本(.NET2.0 3.0 3.5 4.0)區(qū)別6. jscript與vbscript 操作XML元素屬性的代碼7. HTML5實(shí)戰(zhàn)與剖析之觸摸事件(touchstart、touchmove和touchend)8. php使用正則驗(yàn)證密碼字段的復(fù)雜強(qiáng)度原理詳細(xì)講解 原創(chuàng)9. ASP基礎(chǔ)入門第四篇(腳本變量、函數(shù)、過程和條件語(yǔ)句)10. XML入門的常見問題(三)
