python 打印對象的所有屬性值:
def prn_obj(obj): print '/n'.join(['%s:%s' % item for item in obj.__dict__.items()])
Python logger對象屬性(由上述函數(shù)獲取的)
name:get_dataparent:<logging.RootLogger instance at 0x1d8bd88>handlers:[<logging.FileHandler instance at 0x21bcc68>]level:10disabled:1 #當前的logger是否有效,默認為0manager:<logging.Manager instance at 0x1d8bea8>propagate:0 #是否將本級日志filters:[]
部分日志無法輸出
File:logger.conf
[formatters]keys=default [formatter_default]format=%(asctime)s - %(name)s - %(levelname)s - %(message)sclass=logging.Formatter [handlers]keys=console, error_file [handler_console]class=logging.StreamHandlerformatter=defaultargs=tuple() [handler_error_file]class=logging.FileHandlerlevel=INFOformatter=defaultargs=("logger.log", "a") [loggers]keys=root [logger_root]level=DEBUGformatter=defaulthandlers=console,error_file
File:logger.py
#!/bin/env python import loggingfrom logging.config import logging class Test(object): """docstring for Test""" def __init__(self): logging.config.fileConfig("logger.conf") self.logger = logging.getLogger(__name__) def test_func(self): self.logger.error('test_func function') class Worker(object): """docstring for Worker""" def __init__(self): logging.config.fileConfig("logger.conf") self.logger = logging.getLogger(__name__) data_logger = logging.getLogger('data') handler = logging.FileHandler('./data.log') fmt = logging.Formatter('%(asctime)s|%(message)s') handler.setFormatter(fmt) data_logger.addHandler(handler) data_logger.setLevel(logging.DEBUG) self.data_logger = data_logger def test_logger(self): self.data_logger.error("test_logger function") instance = Test() self.data_logger.error("test_logger output") instance.test_func() def main(): worker = Worker() worker.test_logger() if __name__ == '__main__': main()
問題一:測試過程中,只能打印出test_logger function一條語句
問題二:明明只在data_logger中打印出語句,但是logger的日志中也出現(xiàn)了相關(guān)的日志。
問題一解決方案:
利用python -m pdb logger.py 語句對腳本進行調(diào)試發(fā)現(xiàn),在執(zhí)行instance = Test()語句后,通過print '/n'.join(['%s:%s' % item for item in self.data_logger.__dict__.items()])調(diào)試語句看到data_logger的disable屬性值由0變成了True,此時logger的對應屬性也發(fā)生了相同的變化。這種變化導致了logger對象停止記錄日志。參考python logging模塊的相關(guān)手冊發(fā)現(xiàn)“The fileConfig() function takes a default parameter, disable_existing_loggers, which defaults to True for reasons of backward compatibility. This may or may not be what you want, since it will cause any loggers existing before the fileConfig() call to be disabled unless they (or an ancestor) are explicitly named in the configuration.” 的說明,即調(diào)用fileconfig()函數(shù)會將之前存在的所有l(wèi)ogger禁用。在python 2.7版本該fileConfig()函數(shù)添加了一個參數(shù),logging.config.fileConfig(fname, defaults=None, disable_existing_loggers=True),可以顯式的將disable_existing_loggers設置為FALSE來避免將原有的logger禁用。將上述代碼中的Test類中的logging.config.fileConfig函數(shù)改成logging.config.fileConfig("./logger.conf", disable_existing_loggers=0)就可以解決問題。 不過該代碼中由于位于同一程序內(nèi),可以直接用logging.getLogger(LOGGOR_NAME)函數(shù)引用同一個logger,不用再調(diào)用logging.config.fileConfig函數(shù)重新加載一遍了。
新聞熱點
疑難解答
圖片精選