閱讀955 返回首頁    go windows


python日誌__常見日誌格式_用戶指南_日誌服務-阿裏雲

Python日誌簡介

Python的logging模塊提供了通用的日誌係統,可以方便第三方模塊或者是應用使用。這個模塊提供不同的日誌級別,並可以采用不同的方式記錄日誌,比如文件,HTTP GET/POST,SMTP,Socket等,甚至可以自己實現具體的日誌記錄方式。logging模塊與log4j的機製是一樣的,隻是具體的實現細節不同。模塊提供logger,handler,filter,formatter。

日誌的格式在formatter中指定日誌記錄輸出的具體格式。formatter的構造方法需要兩個參數:消息的格式字符串和日期字符串,這兩個參數都是可選的。

Python 日誌使用樣例

import logging  
import logging.handlers  

LOG_FILE = 'tst.log'  

handler = logging.handlers.RotatingFileHandler(LOG_FILE, maxBytes = 1024*1024, backupCount = 5) # 實例化handler   
fmt = '%(asctime)s - %(filename)s:%(lineno)s - %(name)s - %(message)s'  

formatter = logging.Formatter(fmt)   # 實例化formatter  
handler.setFormatter(formatter)      # 為handler添加formatter  

logger = logging.getLogger('tst')    # 獲取名為tst的logger  
logger.addHandler(handler)           # 為logger添加handler  
logger.setLevel(logging.DEBUG)  

logger.info('first info message')  
logger.debug('first debug message')

日誌輸出樣例

2015-03-04 23:21:59,682 - log_test.py:16 - tst - first info message   
2015-03-04 23:21:59,682 - log_test.py:17 - tst - first debug message

關於formatter的配置,采用的是%(key)s的形式,就是字典的關鍵字替換。提供的關鍵字包括:

Format Description
%(name)s Name of the logger (logging channel).
%(levelno)s Numeric logging level for the message (DEBUG, INFO, WARNING, ERROR, CRITICAL).
%(levelname)s Text logging level for the message ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL').
%(pathname)s Full pathname of the source file where the logging call was issued (if available).
%(filename)s Filename portion of pathname.
%(module)s Module (name portion of filename).
%(funcName)s Name of function containing the logging call.
%(lineno)d Source line number where the logging call was issued (if available).
%(created)f Time when the LogRecord was created (as returned by time.time()).
%(relativeCreated)d Time in milliseconds when the LogRecord was created, relative to the time the logging module was loaded.
%(asctime)s Human-readable time when the LogRecord was created. By default this is of the form “2003-07-08 16:49:45,896” (the numbers after the comma are millisecond portion of the time).
%(msecs)d Millisecond portion of the time when the LogRecord was created.
%(thread)d Thread ID (if available).
%(threadName)s Thread name (if available).
%(process)d Process ID (if available).
%(message)s The logged message, computed as msg % args.

Python日誌接入日誌服務樣例

標準步驟按照快速開始文檔的步驟操作。在生成正則式的部分,由於自動生成的正則式隻參考了日誌樣例,無法覆蓋所有的日誌情況,所以需要用戶在自動生成之後做一些微調。

這裏舉一些常見的python日誌的正則式樣例

日誌:
2016-02-19 11:03:13,410 - test.py:19 - tst - first debug message
正則式:
(d+-d+-d+sS+)s+-s+([^:]+):(d+)s+-s+(w+)s+-s+(.*)
日誌配置:
%(asctime)s - %(filename)s:%(lineno)s - %(levelno)s %(levelname)s %(pathname)s %(module)s %(funcName)s %(created)f %(thread)d %(threadName)s %(process)d %(name)s - %(message)s
日誌輸出:
2016-02-19 11:06:52,514 - test.py:19 - 10 DEBUG test.py test <module> 1455851212.514271 139865996687072 MainThread 20193 tst - first debug message
正則式:
(d+-d+-d+sS+)s-s([^:]+):(d+)s+-s+(d+)s+(w+)s+(S+)s+(w+)s+(S+)s+(S+)s+(d+)s+(w+)s+(d+)s+(w+)s+-s+(.*)

最後更新:2016-11-23 17:16:06

  上一篇:go nginx日誌__常見日誌格式_用戶指南_日誌服務-阿裏雲
  下一篇:go log4j日誌__常見日誌格式_用戶指南_日誌服務-阿裏雲