日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Python的logging日志打印

系統 2119 0

Python的print()能夠實現將需要的信息輸出在控制臺上,然而當程序較為復雜,需要將大量的信息記錄下來的時候,print的打印就較為不方便,因此Python引入了logging模塊來記錄一些想要的信息。

logging相對print來說更好控制輸出在哪個地方,怎么輸出及控制消息級別來過濾掉那些不需要的信息。

1.logging的級別關系

先用一個簡單的例子來了解logging的輸出以及各級

            
              logger = logging.getLogger()  
logger.setLevel(logging.NOTSET)
            
          

別之間的關系:

            
              import logging  

logging.debug('this is debug message')  
logging.info('this is info message')  
logging.warning('this is warning message')
logging.error('this is an error message')
logging.critical('this is a critical message')
            
          

其輸出為:

WARNING:root:this is warning message
ERROR:root:this is an error message
CRITICAL:root:this is a critical message

可以看到,只有后面三行有輸出,這是因為logging默認的級別是warning,因此只有warning及其以上的級別能夠顯示出。

logging的級別關系表示為: CRITICAL > ERROR > WARNING > INFO > DEBUG>NOTSET

其每一個級別對應的數值表示為:

Python的logging日志打印_第1張圖片

若要修改logging的輸出,使低于warning的日志也能夠輸出,則可設置level為NOTSET,表示不設置

            
              import logging
  
logging.basicConfig(format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt='%a, %d %b %Y %H:%M:%S',
                    filename='/tmp/test.log',
                    filemode='w')
logger = logging.getLogger()  
logger.setLevel(logging.NOTSET) 
logging.debug('this is debug message')  
logging.info('this is info message')  
logging.warning('this is warning message')
logging.error('this is an error message')
logging.critical('this is a critical message')
            
          

則輸出為:

DEBUG:root:this is debug message
2019-05-22 11:03:25,320 - this is debug message
INFO:root:this is info message
2019-05-22 11:03:25,325 - this is info message
WARNING:root:this is warning message
2019-05-22 11:03:25,329 - this is warning message
ERROR:root:this is an error message
2019-05-22 11:03:25,335 - this is an error message
CRITICAL:root:this is a critical message
2019-05-22 11:03:25,341 - this is a critical message

因此

            
              logger = logging.getLogger()  
logger.setLevel(logging.NOTSET)
            
          

可設置不同的級別,顯示不同程度的log日志

?


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 阿拉善盟| 镇安县| 辽源市| 四平市| 南丹县| 隆尧县| 凌海市| 周口市| 齐齐哈尔市| 江山市| 新乡县| 岳池县| 民和| 安阳市| 兴和县| 辽阳市| 巴林右旗| 南郑县| 乌苏市| 盐池县| 晋中市| 巧家县| 库车县| 黎平县| 德格县| 沙田区| 余姚市| 商丘市| 白水县| 黑水县| 张家口市| 南阳市| 塘沽区| 洛浦县| 巴彦淖尔市| 齐齐哈尔市| 涪陵区| 乌兰浩特市| 珠海市| 安岳县| 宁德市|