【Python】Python中的日誌級別

Python按照重要程度把日誌分爲5個級別,如下:

Python中的日誌級別
級別 日誌函數 描述
DEBUG logging.debug() 最低級別,追蹤問題時使用
INFO logging.info() 記錄程序中一般事件的信息,或確認一切工作正常
WARNING logging.warning() 記錄信息,用於警告
ERROR logging.error() 用於記錄程序報錯信息
CRITICAL logging.critical() 最高級別,記錄可能導致程序崩潰的錯誤

 

 

 

 

 

 

 

可以通過level參數,設置不同的日誌級別。當設置爲高的日誌級別時,低於此級別的日誌不再打印。

五種日誌級別按從低到高排序:

DEBUG < INFO < WARNING < ERROR < CRITICAL

1.  level設置爲DEBUG級別,所有的日誌都會打印

import logging
logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s -%(message)s')
logging.debug('Some debugging details.')
logging.info('The logging module is working')
logging.warning('An error message is about to be logged.')
logging.error('An error has occurred.')
logging.critical('The program is unable to recover!')
 2019-11-17 15:24:30,065 - DEBUG -Some debugging details.
 2019-11-17 15:24:30,074 - INFO -The logging module is working
 2019-11-17 15:24:30,086 - WARNING -An error message is about to be logged.
 2019-11-17 15:24:30,105 - ERROR -An error has occurred.
 2019-11-17 15:24:30,107 - CRITICAL -The program is unable to recover!

2. level設置爲ERROR級別時,只顯示ERROR和CRITICAL日誌

import logging
logging.basicConfig(level=logging.ERROR, format=' %(asctime)s - %(levelname)s -%(message)s')
logging.debug('Some debugging details.')
logging.info('The logging module is working')
logging.warning('An error message is about to be logged.')
logging.error('An error has occurred.')
logging.critical('The program is unable to recover!')
 2019-11-17 15:30:46,767 - ERROR -An error has occurred.
 2019-11-17 15:30:46,768 - CRITICAL -The program is unable to recover!

--The End---

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章