955
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
上一篇:
nginx日誌__常見日誌格式_用戶指南_日誌服務-阿裏雲
下一篇:
log4j日誌__常見日誌格式_用戶指南_日誌服務-阿裏雲
開始混流操作__直播流操作接口_API 手冊_CDN-阿裏雲
格式支持__附錄_API使用手冊_媒體轉碼-阿裏雲
Sqoop__數據傳輸軟件_開發人員指南_E-MapReduce-阿裏雲
步驟4:驗證配置生效__快速入門(網站業務)_DDoS 高防IP-阿裏雲
CreateTable__API 概覽_API 參考_表格存儲-阿裏雲
監控任務概述__創建監控任務_用戶指南_業務實時監控服務 ARMS-阿裏雲
管理生命周期__JavaScript-SDK_SDK 參考_對象存儲 OSS-阿裏雲
查詢鏡像商品類目列表__API參考_雲市場-阿裏雲
設置集群根域名__集群管理_用戶指南_容器服務-阿裏雲
安騎士__安全防護_產品使用手冊_雲解析-阿裏雲
相關內容
常見錯誤說明__附錄_大數據計算服務-阿裏雲
發送短信接口__API使用手冊_短信服務-阿裏雲
接口文檔__Android_安全組件教程_移動安全-阿裏雲
運營商錯誤碼(聯通)__常見問題_短信服務-阿裏雲
設置短信模板__使用手冊_短信服務-阿裏雲
OSS 權限問題及排查__常見錯誤及排除_最佳實踐_對象存儲 OSS-阿裏雲
消息通知__操作指南_批量計算-阿裏雲
設備端快速接入(MQTT)__快速開始_阿裏雲物聯網套件-阿裏雲
查詢API調用流量數據__API管理相關接口_API_API 網關-阿裏雲
使用STS訪問__JavaScript-SDK_SDK 參考_對象存儲 OSS-阿裏雲