Python logging模塊進(jìn)行封裝實(shí)現(xiàn)原理解析
1. 簡(jiǎn)介
追蹤某些軟件運(yùn)行時(shí)所發(fā)生事件的方法, 可以在代碼中調(diào)用日志中某些方法來(lái)記錄發(fā)生的事情
一個(gè)事件可以用一個(gè)可包含可選變量數(shù)據(jù)的消息來(lái)描述
事件有自己的重要性等級(jí)
2. 使用logging日志系統(tǒng)四大組件
- loggers日志器
- 提供應(yīng)用程序代碼直接使用的接口
- handlers處理器
- 用于將日志記錄發(fā)送到指定的目的位置
- filters過(guò)濾器
- 過(guò)濾, 決定哪些輸出哪些日志記錄, 其余忽略
- formatters格式器
- 控制日志輸出格式
使用代碼如下
import os, time, logging, sys from Common.plugs.get_config import r_config BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) if sys.platform == "win32": ENV_CONF_DIR = os.path.join(BASE_DIR, 'Common/conf/env_config.ini').replace('/', '\\') else: ENV_CONF_DIR = os.path.join(BASE_DIR, 'Common/conf/env_config.ini') log_path = r_config(ENV_CONF_DIR, "log", "log_path") class Log: def __init__(self, log_path): self.logName = os.path.join(log_path, '{0}.log'.format(time.strftime('%Y-%m-%d'))) def console_log(self, level, message): # 創(chuàng)建一個(gè)logger logger = logging.getLogger() logger.setLevel(logging.DEBUG) # 創(chuàng)建一個(gè)handler,用于 debug 寫入日志文件 debug_file = logging.FileHandler(self.logName, 'a+', encoding='utf-8') debug_file.setLevel(logging.DEBUG) # 再創(chuàng)建一個(gè)handler,用于輸出到控制臺(tái) ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) # 定義handler的輸出格式 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') debug_file.setFormatter(formatter) ch.setFormatter(formatter) # 給logger添加handler logger.addHandler(debug_file) logger.addHandler(ch) # 記錄一條日志 if level == 'info': logger.info(message) elif level == 'debug': logger.debug(message) elif level == 'warning': logger.warning(message) elif level == 'error': logger.error(message) elif level == 'critical': logger.critical(message) logger.removeHandler(ch) logger.removeHandler(debug_file) debug_file.close() def debug(self, message): #最詳細(xì)日志信息, 多用于問題診斷 self.console_log('debug', message) def info(self, message): #僅次于DEBUG, 多用于記錄關(guān)鍵點(diǎn)信息, 確保程序按預(yù)期執(zhí)行 self.console_log('info', message) def warning(self, message): #低等級(jí)故障, 但程序仍能運(yùn)行, 如磁盤空間不足警告 self.console_log('warning', message) def error(self, message): #由于比WARNING嚴(yán)重的問題, 導(dǎo)致某些功能不能正常運(yùn)行時(shí)的記錄 self.console_log('error', message) def critical(self, message): 嚴(yán)重錯(cuò)誤, 導(dǎo)致應(yīng)用程序不能繼續(xù)運(yùn)行時(shí)的記錄 self.console_log('critical', message) if __name__ == '__main__': Log(log_path).info("adasd") Log(log_path).error("dsadasddasd") '''
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python 日志模塊logging的使用場(chǎng)景及示例
- Python的logging模塊基本用法
- python logging模塊的使用詳解
- python logging模塊的使用
- Python logging模塊handlers用法詳解
- Python logging模塊原理解析及應(yīng)用
- Python logging日志模塊 配置文件方式
- Python logging模塊異步線程寫日志實(shí)現(xiàn)過(guò)程解析
- Python logging模塊寫入中文出現(xiàn)亂碼
- Python日志處理模塊logging用法解析
- python將logging模塊封裝成單獨(dú)模塊并實(shí)現(xiàn)動(dòng)態(tài)切換Level方式
- python 日志 logging模塊詳細(xì)解析
- 多個(gè)python文件調(diào)用logging模塊報(bào)錯(cuò)誤
- Python基礎(chǔ)之logging模塊知識(shí)總結(jié)
相關(guān)文章
python爬蟲請(qǐng)求頁(yè)面urllib庫(kù)詳解
這篇文章主要介紹了python爬蟲請(qǐng)求頁(yè)面urllib庫(kù)詳解,python3將urllib和urllib2模塊整合并命名為urllib模塊,urllib模塊有多個(gè)子模塊,各有不同的功能,需要的朋友可以參考下2023-07-07Python astype(np.float)函數(shù)使用方法解析
這篇文章主要介紹了Python astype(np.float)函數(shù)使用方法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06pytorch 獲取層權(quán)重,對(duì)特定層注入hook, 提取中間層輸出的方法
今天小編就為大家分享一篇pytorch 獲取層權(quán)重,對(duì)特定層注入hook, 提取中間層輸出的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08全面解析Python的While循環(huán)語(yǔ)句的使用方法
這篇文章主要介紹了全面解析Python的While循環(huán)語(yǔ)句的使用方法,是Python入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-10-10pandas 按照特定順序輸出的實(shí)現(xiàn)代碼
這篇文章主要介紹了pandas 按照特定順序輸出的實(shí)現(xiàn)代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07python3.6+selenium實(shí)現(xiàn)操作Frame中的頁(yè)面元素
這篇文章主要為大家詳細(xì)介紹了python3.6+selenium實(shí)現(xiàn)操作Frame中的頁(yè)面元素,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07PyTorch之前向傳播函數(shù)forward用法解讀
這篇文章主要介紹了PyTorch之前向傳播函數(shù)forward用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09