国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

python將logging模塊封裝成單獨模塊并實現動態切換Level方式

瀏覽:66日期:2022-07-25 17:06:01

查找了很多資料,但網上給出的教程都是大同小異的,而我想將代碼進一步精簡,解耦,想實現如下兩個目標

1. 將logging模塊的初始化,配置,設置等代碼封裝到一個模塊中;

2. 能根據配置切換logging.level, 網上給出的教程都是寫死的,如果我在線上之前使用了logging.info(msg),現在想切換為logging.debug(msg)怎么辦?需要能夠根據配置文件中的 設置配置logging.level

兩個文件:

logging_class:將logging模塊的初始化,配置,設置等代碼封裝到一此模塊中,讀取配置文件中對于log等級的設置項;需要使用log功能的模塊import 這個模塊

applogconfig.ini: 配置文件

logging_class:

import loggingimport sysimport ConfigParser def log_building(log_file): try: #set format format_str=logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s') #create stander output handler crit_hand=logging.StreamHandler(sys.stderr) crit_hand.setFormatter(format_str) #create file handler file_hand=logging.FileHandler(log_file,’a’) file_hand.setFormatter(format_str) app_log=logging.getLogger(__name__) app_log.addHandler(crit_hand) app_log.addHandler(file_hand) #必須設置,否則無法輸出 app_log.setLevel(logging.NOTSET) return app_log except Exception as e: logging.shutdown() raise e def config_file_get(fpath): try: cnf_dict={} cfg=ConfigParser.SafeConfigParser() cfg.read(fpath) for section in cfg.sections(): #將ini中的item組合到字典中,key=section+_option for item in cfg.items(section): key= section+’_’+item[0] value=item[1] if cnf_dict.get(key,None)==None: cnf_dict[key]=value return cnf_dict except Exception as e: raise edef log_level_get(level): DEBUG_LEVEL={’CRITICAL’:logging.CRITICAL,’ERROR’:logging.ERROR,’WARNING’:logging.WARNING, ’INFO’:logging.INFO,’DEBUG’:logging.DEBUG } try: return DEBUG_LEVEL.get(level.upper()) except Exception as e: raise e

applogconfig.ini內容:

[log]log_level=ERRORdir=log

以下為unittest內容:

import unittestimport logging_classimport osimport logging class Test(unittest.TestCase): cfg={} def setUp(self): print ’test begin’ self.cfg={} def tearDown(self): print ’test end’ def testlog_level_get(self): currentWorkingPath = r’E:Myworkspacepythonlogging_modulelogging_module’ ini_file=os.path.normpath(os.path.abspath(os.path.join(currentWorkingPath,’applogconfig.ini’))) self.cfg=logging_class.config_file_get(ini_file) self.assertEqual(self.cfg[’log_log_level’].upper(), ’ERROR’, ’OK’) def testlog_level_set(self): currentWorkingPath = r’E:Myworkspacepythonlogging_modulelogging_module’ ini_file=os.path.normpath(os.path.abspath(os.path.join(currentWorkingPath,’applogconfig.ini’))) self.cfg=logging_class.config_file_get(ini_file) #print self.cfg[’log_log_level’] self.assertEqual(logging_class.log_level_get(self.cfg[’log_log_level’]), logging.ERROR, ’OK’) def testlog_building(self): currentWorkingPath = r’E:Myworkspacepythonlogging_modulelogging_module’ ini_file=os.path.normpath(os.path.abspath(os.path.join(currentWorkingPath,’applogconfig.ini’))) log_file=os.path.normpath(os.path.abspath(os.path.join(currentWorkingPath,’b.log’))) self.cfg=logging_class.config_file_get(ini_file) #print self.cfg[’log_log_level’] level=logging_class.log_level_get(self.cfg[’log_log_level’]) log=logging_class.log_building(log_file) log.log(level, ’dddds’) log.debug(’msg’) if __name__ == '__main__': #import sys;sys.argv = [’’, ’Test.testName’] unittest.main()

輸出:

Finding files... done.Importing test modules ... done.test begintest endtest begintest endtest begin2016-12-15 17:59:04,059 logging_module_test.py[line:48] ERROR ddddstest end----------------------------------------------------------------------Ran 3 tests in 0.004s

補充知識:一種logging封裝方法,不會產生重復log

在調試logging的封裝的時候,發現已經調用了logging封裝的函數,在被其它函數再調用時,會出現重復的logging。原因是不同的地方創建了不同的handler,所以會重復,可以使用暴力方法解決

暴力方式就是每次創建新的對象就清空logger.handlers

我常用的封裝如下

import loggingimport time,os’’’ 使用方法: import mylog log = mylog.Log().getlog() log.debug('###')’’’class Log(): def __init__(self,logger='mylog'): self.logger = logging.getLogger(logger) self.logger.setLevel(logging.DEBUG) self.log_time = ''+time.strftime('%Y-%m-%d_%H_%M', time.localtime())+'.log' # 在進程路徑創建log文件夾 # self.log_path = os.path.join(os.getcwd() + 'log') # 固定在mylog上一級創建 self.log_path = os.path.join(os.path.dirname(os.path.dirname(__file__)) + 'log') if os.path.exists(self.log_path) and os.path.isdir(self.log_path): pass else: os.makedirs(self.log_path) self.log_name = os.path.join(self.log_path + self.log_time) #因為多出調用logger會生成多個handlers,所以每次調用清空handler self.logger.handlers = [] fh = logging.FileHandler(self.log_name, ’a’, encoding=’utf-8’) formatter = logging.Formatter(’[%(levelname)s][%(asctime)s] [%(filename)s]->[%(funcName)s] line:%(lineno)d ---> %(message)s’) fh.setLevel(logging.DEBUG) fh.setFormatter(formatter) self.logger.addHandler(fh) ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) ch.setFormatter(formatter) self.logger.addHandler(ch) fh.close() def getlog(self): return self.loggerif __name__ == '__main__': log = Log().getlog() log.debug('hello')

以上這篇python將logging模塊封裝成單獨模塊并實現動態切換Level方式就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 平原县| 永济市| 水城县| 女性| 万荣县| 江孜县| 来凤县| 通州区| 普洱| 大同县| 海林市| 潍坊市| 巍山| 安乡县| 齐齐哈尔市| 梁河县| 于都县| 高尔夫| 集贤县| 板桥市| 徐州市| 临高县| 新乐市| 紫金县| 呈贡县| 孟连| 宁河县| 望谟县| 庄浪县| 黄大仙区| 偃师市| 香港| 商河县| 满洲里市| 清河县| 乌拉特中旗| 民丰县| 株洲市| 汝城县| 华亭县| 修武县|