python實(shí)現(xiàn)log日志的示例代碼
更新時(shí)間:2018年04月28日 08:44:47 作者:shengnan_only
下面小編就為大家分享一篇python實(shí)現(xiàn)log日志的示例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
源代碼:
# coding=utf-8 import logging import os import time LEVELS={'debug':logging.DEBUG,\ 'info':logging.INFO,\ 'warning':logging.WARNING,\ 'error':logging.ERROR,\ 'critical':logging.CRITICAL,} logger=logging.getLogger() level='default' def createFile(filename): path=filename[0:filename.rfind('/')] if not os.path.isdir(path): os.makedirs(path) if not os.path.isfile(filename): #創(chuàng)建并打開一個(gè)新文件 fd = open(filename,mode='w',encoding='utf-8') fd.close() class MyLog: log_filename='E:/quality/it/pyrequest-master/log/itest.log' err_filename='E:/quality/it/pyrequest-master/log/err.log' dateformat='%Y-%m-%d %H:%M:%S' logger.setLevel(LEVELS.get(level,logging.NOTSET)) createFile(log_filename) createFile(err_filename) #注意文件內(nèi)容寫入時(shí)編碼格式指定 handler=logging.FileHandler(log_filename,encoding='utf-8') errhandler=logging.FileHandler(err_filename,encoding='utf-8') @staticmethod #靜態(tài)方法 def debug(log_message): setHandler('debug') logger.debug("[DEBUG "+getCurrentTime()+"]"+log_message) removerhandler('debug') @staticmethod def info(log_message): setHandler('info') logger.info("[INFO "+getCurrentTime()+"]"+log_message) removerhandler('info') @staticmethod def warning(log_message): setHandler('warning') logger.warning("[WARNING "+getCurrentTime()+"]"+log_message) removerhandler('warning') @staticmethod def error(log_message): setHandler('error') logger.error("[ERROR "+getCurrentTime()+"]"+log_message) removerhandler('error') @staticmethod def critical(log_message): setHandler('critical') logger.critical("[CRITICAL "+getCurrentTime()+"]"+log_message) removerhandler('critical') # logger可以看做是一個(gè)記錄日志的人,對(duì)于記錄的每個(gè)日志,他需要有一套規(guī)則,比如記錄的格式(formatter), # 等級(jí)(level)等等,這個(gè)規(guī)則就是handler。使用logger.addHandler(handler)添加多個(gè)規(guī)則, # 就可以讓一個(gè)logger記錄多個(gè)日志。 def setHandler(level): if level=='error': logger.addHandler(MyLog.errhandler) #handler=logging.FileHandler(log_filename) #把logger添加上handler logger.addHandler(MyLog.handler) def removerhandler(level): if level=='error': logger.removeHandler(MyLog.errhandler) logger.removeHandler(MyLog.handler) def getCurrentTime(): return time.strftime(MyLog.dateformat,time.localtime(time.time())) if __name__=="__main__": MyLog.debug("This is debug message") MyLog.info("This is info message") MyLog.warning("This is warning message") MyLog.error("This is error message") MyLog.critical("This is critical message")
以上這篇python實(shí)現(xiàn)log日志的示例代碼就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)FTP文件定時(shí)自動(dòng)下載的步驟
這篇文章主要介紹了Python實(shí)現(xiàn)FTP文件定時(shí)自動(dòng)下載的示例,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-12-12解決python3 整數(shù)數(shù)組轉(zhuǎn)bytes的效率問題
這篇文章主要介紹了解決python3 整數(shù)數(shù)組轉(zhuǎn)bytes的效率問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03Python高級(jí)編程之消息隊(duì)列(Queue)與進(jìn)程池(Pool)實(shí)例詳解
這篇文章主要介紹了Python高級(jí)編程之消息隊(duì)列(Queue)與進(jìn)程池(Pool),結(jié)合實(shí)例形式詳細(xì)分析了Python消息隊(duì)列與進(jìn)程池的相關(guān)原理、使用技巧與操作注意事項(xiàng),需要的朋友可以參考下2019-11-11Python利用解析JSON實(shí)現(xiàn)主機(jī)管理
JSON 是一種獨(dú)立于編程語(yǔ)言的數(shù)據(jù)格式,因此在不同的編程語(yǔ)言中都有對(duì)應(yīng)的解析器和生成器,本文主要介紹了Python如何通過解析JSON實(shí)現(xiàn)主機(jī)管理,感興趣的小伙伴可以了解一下2023-12-12python自然語(yǔ)言處理之字典樹知識(shí)總結(jié)
這篇文章主要介紹了python自然語(yǔ)言處理之字典樹知識(shí)總結(jié),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04Python 實(shí)現(xiàn)靜態(tài)鏈表案例詳解
這篇文章主要介紹了Python 實(shí)現(xiàn)靜態(tài)鏈表案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09