Python基礎文法 - logging

來源:互聯網
上載者:User

標籤:r檔案   標準   cti   訪問   debug   inf   層級   test   root   

描述:

  很多程式都有記錄日誌的需求,並且日誌中包含的資訊即有正常的程式訪問日誌,還可能有錯誤、警告等資訊輸出,python的logging模組提供了標準的日誌介面,你可以通過它儲存各種格式的日誌。

  logging的日誌可以分為5個層級:

  • debug()
  • info()
  • warning()
  • error()
  • critical()
簡單用法:
logging.debug(‘degbug message‘)logging.info(‘info message‘)logging.warning(‘hello‘)logging.error(‘error message‘)logging.critical(‘critical message‘)>>>輸出:WARNING:root:helloERROR:root:error messageCRITICAL:root:critical message
level = warning
logger檔案:
import logginglogging.basicConfig(level = logging.DEBUG,                    format = ‘%(asctime)s %(filename)s - %(levelname)s - %(message)s‘,                    datefmt= ‘%a,%d %b %Y %H:%M:%S‘,                    filename = ‘test.log‘,                    filemode = ‘w‘)logging.debug(‘degbug message‘)logging.info(‘info message‘)logging.warning(‘hello‘)logging.error(‘error message‘)logging.critical(‘critical message‘)>>>輸出:test.logWed,21 Mar 2018 22:58:03 logging_module.py - DEBUG - degbug messageWed,21 Mar 2018 22:58:03 logging_module.py - INFO - info messageWed,21 Mar 2018 22:58:03 logging_module.py - WARNING - helloWed,21 Mar 2018 22:58:03 logging_module.py - ERROR - error messageWed,21 Mar 2018 22:58:03 logging_module.py - CRITICAL - critical message

 

Python基礎文法 - logging

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.