Python基礎-logging模組

來源:互聯網
上載者:User

標籤:輸出   import   pre   mha   方式   error   proc   情況   pen   

日誌的層級
import logginglogging.debug(‘debug message‘)logging.info(‘info message‘)logging.warning(‘warning message‘)logging.error(‘error message‘)logging.critical(‘critical message‘)執行結果:WARNING:root:warning messageERROR:root:error messageCRITICAL:root:critical message
    預設情況下Python的logging模組將日誌列印到了標準輸出中,且只顯示了大於等於WARNING層級的日誌,這說明預設的記錄層級設定為
WARNING(記錄層級等級CRITICAL>ERROR>WARNING>INFO>DEBUG>NOTSET),
預設的日誌格式為:
記錄層級:Logger名稱:使用者輸出資訊

記錄層級的配置,日誌格式,輸出位置
# 自訂日誌的層級
import logging
logging.basicConfig(level=logging.DEBUG, format=‘%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s‘, datefmt=‘%a, %d %b %Y %H:%M:%S‘, filename=‘test.log‘, filemode=‘a‘ )logging.debug(‘debug message‘)logging.info(‘info message‘)logging.warning(‘warning message‘)logging.error(‘error message‘)logging.critical(‘critical message‘)執行結果會在工作目錄下產生一個test.log檔案,內容如下:Tue, 17 Oct 2017 14:08:22 test.py[line:13] DEBUG debug messageTue, 17 Oct 2017 14:08:22 test.py[line:14] INFO info messageTue, 17 Oct 2017 14:08:22 test.py[line:15] WARNING warning messageTue, 17 Oct 2017 14:08:22 test.py[line:16] ERROR error messageTue, 17 Oct 2017 14:08:22 test.py[line:17] CRITICAL critical message
logging.basicConfig()函數中的參數及說明如下:
filename:用指定的檔案名稱建立FiledHandler(後邊會具體講解handler的概念),這樣日誌會被儲存在指定的檔案中。
filemode:檔案開啟檔案,在指定了filename時使用這個參數,預設值為“a”還可指定為“w”。
format:指定handler使用的日誌顯示格式。
datefmt:指定日期時間格式。
level:設定rootlogger(後邊會講解具體概念)的記錄層級
stream:用指定的stream建立StreamHandler。可以指定輸出到sys.stderr,sys.stdout或者檔案(f=open(‘test.log‘,‘w‘)),預設為sys.stderr。若同時列出了filename和stream兩個參數,則stream參數會被忽略。
format參數中可能用到的格式化串:
%(name)s Logger的名字
%(levelno)s 數字形式的記錄層級
%(levelname)s 文本形式的記錄層級
%(pathname)s 調用日誌輸出函數的模組的完整路徑名,可能沒有
%(filename)s 調用日誌輸出函數的模組的檔案名稱
%(module)s 調用日誌輸出函數的模組名
%(funcName)s 調用日誌輸出函數的函數名
%(lineno)d 調用日誌輸出函數的語句所在的程式碼
%(created)f 目前時間,用UNIX標準的表示時間的浮 點數表示
%(relativeCreated)d 輸出日誌資訊時的,自Logger建立以 來的毫秒數
%(asctime)s 字串形式的目前時間。預設格式是 “2003-07-08 16:49:45,896”。逗號後面的是毫秒
%(thread)d 線程ID。可能沒有
%(threadName)s 線程名。可能沒有
%(process)d 進程ID。可能沒有
%(message)s使用者輸出的訊息

將日誌同時輸出到螢幕和檔案
logger = logging.getLogger()fh = logging.FileHandler(‘test.log‘)ch = logging.StreamHandler()formatter = logging.Formatter(‘%(asctime)s - %(name)s - %(levelname)s - %(message)s‘)fh.setFormatter(formatter)ch.setFormatter(formatter)logger.setLevel(logging.DEBUG)logger.addHandler(fh)logger.addHandler(ch)logging.debug(‘debug message‘)logging.info(‘info message‘)logging.warning(‘warning message‘)logging.error(‘error message‘)logging.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.