Python的 logging
模塊提供了靈活的日誌處理相關功能, 能夠用來追蹤程序運行的狀況。html
logging
模塊提供了一系列標準的日誌等級: DEBUG, INFO, WARNING, ERROR, CRITICAL
, 顧名思義能夠大體看出它們各自的使用狀況。 logging
模塊設置的默認等級時 WARNING
, 這意味着默認狀況下,日誌級別爲 WARNING, ERROR, CRITICAL
的日誌會被記錄,而 DEBUG, INFO
的日誌會被忽略。python
不一樣等級的value值以下,只有當value大於 logger
的值纔會記錄日誌。安全
Level Value CRITICAL 50 ERROR 40 WARNING 30 INFO 20 DEBUG 10 UNSET 0
下面看一個簡單的官方文檔上的例子:app
import logging logging.warning('Watch out!') # will print a message to the console logging.info('I told you so') # will not print anything
輸出以下:this
WARNING:root:Watch out!
能夠看到info記錄的信息沒有輸出,這是由於默認輸出級別不低於WARNING級別的。線程
logging
支持輸出日誌到文件,參考下面示例:debug
import logging logging.basicConfig(filename='example.log',level=logging.DEBUG) logging.debug('This message should go to the log file') logging.info('So should this') logging.warning('And this, too')
其中level是指的記錄等級, 輸出以下:日誌
DEBUG:root:This message should go to the log file INFO:root:So should this WARNING:root:And this, too
logger是logging模塊提供的日誌類 Logger
的實例,它暴露出接口能夠直接供程序調用。
每一個實例都有一個名字,而且示例間有類之間那種繼承關係,根據logger的名字來區分,好比叫"scan"的logger是叫"scan.text"和"scan.html"的父類(沒錯,他們是以點號錯分隔符)。code
全部logger共同的父類是 root
, 就是上面示例中的中間那個默認的root。 basicConfig
默認的輸出格式爲: severity:logger name:message
。orm
logger的經過 logging.getLogger(name)
來建立,有種在包裏命名的慣用作法是:
logger = logging.getLogger(__name__)
這樣的好處是能夠從logger的名字清楚的看到記錄的來源。
handlers
承擔 logging
模塊裏負責處理合適的信息到不一樣的地方的角色,下面經過設置一個RotatingFileHandler來展現handler的特性。
有時候須要建立多個輪轉日誌,每一個日誌保存必定長度的內容,最多保留必定數量的日誌,其他的丟棄,這種狀況下,能夠定義 RotatingFileHandler
來實現:
logging_rotatingfile_example.py import glob import logging import logging.handlers LOG_FILENAME = 'logging_rotatingfile_example.out' # Set up a specific logger with our desired output level my_logger = logging.getLogger('MyLogger') my_logger.setLevel(logging.DEBUG) # Add the log message handler to the logger handler = logging.handlers.RotatingFileHandler( LOG_FILENAME, maxBytes=20, backupCount=5, ) my_logger.addHandler(handler) # Log some messages for i in range(20): my_logger.debug('i = %d' % i) # See what files are created logfiles = glob.glob('%s*' % LOG_FILENAME) for filename in logfiles: print(filename)
運行輸出以下:
logging_rotatingfile_example.out logging_rotatingfile_example.out.1 logging_rotatingfile_example.out.2 logging_rotatingfile_example.out.3 logging_rotatingfile_example.out.4 logging_rotatingfile_example.out.5
當日志內容達到定義的 maxBytes
時,會自動重命名文件後加上後綴".1",若是已經存在後續的".1",".2"等則自動重命名他們向後加1,最後最多隻保留 backupCount
定義數量的日誌文件。
其它有用的handler參見這裏。
Formatters
能夠用來控制日誌輸出的格式,參考下面的示例:
import logging # create logger logger = logging.getLogger('simple_example') logger.setLevel(logging.DEBUG) # create console handler and set level to debug ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) # create formatter formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') # add formatter to ch ch.setFormatter(formatter) # add ch to logger logger.addHandler(ch) # 'application' code logger.debug('debug message') logger.info('info message') logger.warn('warn message') logger.error('error message') logger.critical('critical message')
輸出以下:
2016-11-27 23:18:51,128 - simple_example - DEBUG - debug message 2016-11-27 23:18:51,128 - simple_example - INFO - info message 2016-11-27 23:18:51,128 - simple_example - WARNING - warn message 2016-11-27 23:18:51,128 - simple_example - ERROR - error message 2016-11-27 23:18:51,128 - simple_example - CRITICAL - critical message
能夠看到 %(asctime)s - %(name)s - %(levelname)s - %(message)s
這裏對格式化輸出的影響。
其中默認的日期時間顯示的格式是ISO8601格式, 也能夠自定義時間格式,以下面的例子:
import logging logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') logging.warning('is when this event was logged.')
輸出:
python test12.py 11/27/2016 11:22:41 PM is when this event was logged.
好有個比較有用的格式化參數時 %(lineno)d
, 顯示logger調用的時候所處的行數。具體的格式和做用能夠參見這裏。
以 ERROR
的等級記錄日誌,但和 DEBUG
等級同樣會輸出詳細的錯誤信息,一般用在exception處理中
Filters 是能夠被handlers和loggers用來過濾日誌的輸出的,由於用的很少,具體可參見文檔。
logging模塊是經過線程鎖保證線程安全的。
官方文檔上看到的logging流程圖,能夠幫助理解日誌記錄流程,參見這裏。
參見這裏。