955
阿里云
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-阿里云