python中的日志級別


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

可以通過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!
  1. 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!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM