最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python日志記錄模塊實(shí)例及改進(jìn)

 更新時(shí)間:2017年02月12日 14:18:05   作者:sxhlinux  
許多應(yīng)用程序中都會(huì)有日志模塊,用于記錄系統(tǒng)在運(yùn)行過(guò)程中的一些關(guān)鍵信息,以便于對(duì)系統(tǒng)的運(yùn)行狀況進(jìn)行跟蹤。在python中,我們不需要第三方的日志組件,因?yàn)樗呀?jīng)為我們提供了簡(jiǎn)單易用、且功能強(qiáng)大的日志模塊:logging。

python 打印對(duì)象的所有屬性值:

def prn_obj(obj):
  print '\n'.join(['%s:%s' % item for item in obj.__dict__.items()])

Python logger對(duì)象屬性(由上述函數(shù)獲取的)

name:get_data
parent:<logging.RootLogger instance at 0x1d8bd88>
handlers:[<logging.FileHandler instance at 0x21bcc68>]
level:10
disabled:1   #當(dāng)前的logger是否有效,默認(rèn)為0
manager:<logging.Manager instance at 0x1d8bea8>
propagate:0   #是否將本級(jí)日志
filters:[]
 

部分日志無(wú)法輸出

File:logger.conf

 
[formatters]
keys=default
 
[formatter_default]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
class=logging.Formatter
 
[handlers]
keys=console, error_file
 
[handler_console]
class=logging.StreamHandler
formatter=default
args=tuple()
 
[handler_error_file]
class=logging.FileHandler
level=INFO
formatter=default
args=("logger.log", "a")
 
[loggers]
keys=root
 
[logger_root]
level=DEBUG
formatter=default
handlers=console,error_file

File:logger.py

 
#!/bin/env python
 
import logging
from 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()
 

問(wèn)題一:測(cè)試過(guò)程中,只能打印出test_logger function一條語(yǔ)句
問(wèn)題二:明明只在data_logger中打印出語(yǔ)句,但是logger的日志中也出現(xiàn)了相關(guān)的日志。

問(wèn)題一解決方案:

利用python -m pdb logger.py 語(yǔ)句對(duì)腳本進(jìn)行調(diào)試發(fā)現(xiàn),在執(zhí)行instance = Test()語(yǔ)句后,通過(guò)print '\n'.join(['%s:%s' % item for item in self.data_logger.__dict__.items()])調(diào)試語(yǔ)句看到data_logger的disable屬性值由0變成了True,此時(shí)logger的對(duì)應(yīng)屬性也發(fā)生了相同的變化。這種變化導(dǎo)致了logger對(duì)象停止記錄日志。參考python  logging模塊的相關(guān)手冊(cè)發(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.” 的說(shuō)明,即調(diào)用fileconfig()函數(shù)會(huì)將之前存在的所有l(wèi)ogger禁用。在python 2.7版本該fileConfig()函數(shù)添加了一個(gè)參數(shù),logging.config.fileConfig(fname, defaults=None, disable_existing_loggers=True),可以顯式的將disable_existing_loggers設(shè)置為FALSE來(lái)避免將原有的logger禁用。將上述代碼中的Test類中的logging.config.fileConfig函數(shù)改成logging.config.fileConfig("./logger.conf", disable_existing_loggers=0)就可以解決問(wèn)題。 不過(guò)該代碼中由于位于同一程序內(nèi),可以直接用logging.getLogger(LOGGOR_NAME)函數(shù)引用同一個(gè)logger,不用再調(diào)用logging.config.fileConfig函數(shù)重新加載一遍了。

問(wèn)題二解決方案:

logger對(duì)象有個(gè)屬性propagate,如果這個(gè)屬性為True,就會(huì)將要輸出的信息推送給該logger的所有上級(jí)logger,這些上級(jí)logger所對(duì)應(yīng)的handlers就會(huì)把接收到的信息打印到關(guān)聯(lián)的日志中。logger.conf配置文件中配置了相關(guān)的root logger的屬性,這個(gè)root logger就是默認(rèn)的logger日志。
 
修改后的如下:

File:logger.conf

 
[formatters]
keys=default, data
 
[formatter_default]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
class=logging.Formatter
 
[formatter_data]
format=%(asctime)s|%(message)s
class=logging.Formatter
 
[handlers]
keys=console, error_file, data_file
 
[handler_console]
class=logging.StreamHandler
formatter=default
args=tuple()
 
[handler_error_file]
class=logging.FileHandler
level=INFO
formatter=default
args=("logger.log", "a")
 
[handler_data_file]
class=logging.FileHandler
level=INFO
formatter=data
args=("data_new.log", "a")
 
[loggers]
keys=root, data
 
[logger_root]
level=DEBUG
handlers=console,error_file
 
[logger_data]
level=DEBUG
handlers=data_file
qualname=data
propagate=0
 

File:logger.py

 
#!/bin/env python
 
import logging
from logging.config import logging
 
class Test(object):
  """docstring for Test"""
  def __init__(self):
    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__)
    self.data_logger = logging.getLogger('data')
 
  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()

相關(guān)文章

  • Python xlwings插入Excel圖片的實(shí)現(xiàn)方法

    Python xlwings插入Excel圖片的實(shí)現(xiàn)方法

    這篇文章主要介紹了Python xlwings插入Excel圖片的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • Selenium向iframe富文本框輸入內(nèi)容過(guò)程圖解

    Selenium向iframe富文本框輸入內(nèi)容過(guò)程圖解

    這篇文章主要介紹了Selenium向iframe富文本框輸入內(nèi)容過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • python貪婪匹配以及多行匹配的實(shí)例講解

    python貪婪匹配以及多行匹配的實(shí)例講解

    下面小編就為大家分享一篇python貪婪匹配以及多行匹配的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • 使用Pycharm創(chuàng)建一個(gè)Django項(xiàng)目的超詳細(xì)圖文教程

    使用Pycharm創(chuàng)建一個(gè)Django項(xiàng)目的超詳細(xì)圖文教程

    Django是比較經(jīng)典的Python web框架,最近剛好在項(xiàng)目中用到了Django,所以下面這篇文章主要給大家介紹了關(guān)于使用Pycharm創(chuàng)建一個(gè)Django項(xiàng)目的超詳細(xì)圖文教程,文中介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08
  • python從Hadoop?HDFS導(dǎo)出數(shù)據(jù)到關(guān)系數(shù)據(jù)庫(kù)

    python從Hadoop?HDFS導(dǎo)出數(shù)據(jù)到關(guān)系數(shù)據(jù)庫(kù)

    這篇文章主要為大家詳細(xì)介紹了Python如何從Hadoop?HDFS中導(dǎo)出數(shù)據(jù)并通過(guò)DataX工具導(dǎo)入到關(guān)系數(shù)據(jù)庫(kù),例如MySQL,Oracle,PostgreSQL等,感興趣的可以了解下
    2024-11-11
  • Python的Geopy庫(kù)處理地理編碼與位置信息

    Python的Geopy庫(kù)處理地理編碼與位置信息

    地理編碼和位置信息在現(xiàn)代應(yīng)用中扮演著重要角色,本文主要介紹了Python的Geopy庫(kù)處理地理編碼與位置信息,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-12-12
  • 詳解Python Matplot中文顯示完美解決方案

    詳解Python Matplot中文顯示完美解決方案

    這篇文章主要介紹了Python Matplot中文顯示完美解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Python操作mongodb數(shù)據(jù)庫(kù)的方法詳解

    Python操作mongodb數(shù)據(jù)庫(kù)的方法詳解

    這篇文章主要介紹了Python操作mongodb數(shù)據(jù)庫(kù)的方法,結(jié)合實(shí)例形式詳細(xì)分析了Python下載、安裝pymongo及操作MongoDB數(shù)據(jù)庫(kù)相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2018-12-12
  • Python中各類Excel表格批量合并問(wèn)題的實(shí)現(xiàn)思路與案例

    Python中各類Excel表格批量合并問(wèn)題的實(shí)現(xiàn)思路與案例

    在日常工作中,可能會(huì)遇到各類表格合并的需求。本文主要介紹了Python中各類Excel表格批量合并問(wèn)題的實(shí)現(xiàn)思路與案例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • python強(qiáng)大的hook函數(shù)使用及應(yīng)用場(chǎng)景

    python強(qiáng)大的hook函數(shù)使用及應(yīng)用場(chǎng)景

    鉤子函數(shù)(hook function)是把我們自己實(shí)現(xiàn)的hook函數(shù)在某一時(shí)刻掛接到目標(biāo)掛載點(diǎn)上,這篇文章主要介紹了python強(qiáng)大的hook函數(shù)使用及應(yīng)用場(chǎng)景,需要的朋友可以參考下
    2023-05-05

最新評(píng)論

昭觉县| 平乐县| 平乡县| 安康市| 福安市| 蕉岭县| 南川市| 诸城市| 随州市| 天全县| 潞西市| 自治县| 龙胜| 南溪县| 共和县| 台山市| 独山县| 叶城县| 藁城市| 衡南县| 韶关市| 株洲市| 东明县| 将乐县| 泰来县| 神池县| 西充县| 惠水县| 泸溪县| 玉山县| 安义县| 清苑县| 新野县| 泉州市| 南部县| 连南| 鄢陵县| 盐边县| 长治市| 三明市| 高要市|