python logging 重復(fù)寫(xiě)日志問(wèn)題解決辦法詳解
python logging 重復(fù)寫(xiě)日志問(wèn)題
用Python的logging模塊記錄日志時(shí),遇到了重復(fù)記錄日志的問(wèn)題,第一條記錄寫(xiě)一次,第二條記錄寫(xiě)兩次,第三條記錄寫(xiě)三次。。。很頭疼,這樣記日志可不行。網(wǎng)上搜索到了原因與解決方案:
原因:沒(méi)有移除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,以此類(lèi)推,調(diào)用幾次就會(huì)有幾個(gè)handler。。
所以這里有以下幾個(gè)解決辦法:
每次創(chuàng)建不同name的logger,每次都是新logger,不會(huì)有添加多個(gè)handler的問(wèn)題。(ps:這個(gè)辦法太笨,不過(guò)我之前就是這么干的。。) 像上面一樣每次記錄完日志之后,調(diào)用removeHandler()把這個(gè)logger里的handler移除掉。在log方法里做判斷,如果這個(gè)logger已有handler,則不再添加handler。 與方法2一樣,不過(guò)把用pop把logger的handler列表中的handler移除。下面是方法3與方法4的代碼示例:
方法3:
import loggingdef log(message): logger = logging.getLogger(’testlog’) # 這里進(jìn)行判斷,如果logger.handlers列表為空,則添加,否則,直接去寫(xiě)日志 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è)人覺(jué)得方法3判斷更加優(yōu)雅,你覺(jué)得呢?
到此這篇關(guān)于python logging 重復(fù)寫(xiě)日志問(wèn)題j解決辦法詳解的文章就介紹到這了,更多相關(guān)python logging 重復(fù)寫(xiě)日志問(wèn)題內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)2. 使用Spry輕松將XML數(shù)據(jù)顯示到HTML頁(yè)的方法3. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究4. XHTML 1.0:標(biāo)記新的開(kāi)端5. ASP基礎(chǔ)知識(shí)VBScript基本元素講解6. 利用CSS3新特性創(chuàng)建透明邊框三角7. XML入門(mén)的常見(jiàn)問(wèn)題(四)8. asp(vbscript)中自定義函數(shù)的默認(rèn)參數(shù)實(shí)現(xiàn)代碼9. 詳解CSS偽元素的妙用單標(biāo)簽之美10. HTML5 Canvas繪制圖形從入門(mén)到精通
