阅读955 返回首页    go 阿里云


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日志__常见日志格式_用户指南_日志服务-阿里云