一个提供合理日志输出格式的Python小库
项目描述
_ ___ ___ ___ _ _ _ | | / _ \ / __/ __| \| | /_\ | |_| (_) | (_ \__ \ .` |/ _ \ |____\___/ \___|___/_|\_/_/ \_\
logsna 是一个提供合理日志输出格式的Python小库。
安装
$ [sudo] pip install logsna
或者从git master分支的尖端版本
$ [sudo] pip install git+https://github.com/rspivak/logsna.git#egg=logsna
如何使用它
logsna 提供一个自定义格式化类 logsna.Formatter,可以在日志配置文件中使用
# sanefmt.py
import logging
import logging.config
from StringIO import StringIO
CONFIG = """\
[loggers]
keys=root
[handlers]
keys=console
[handler_console]
class=logging.StreamHandler
args=(sys.stderr,)
formatter=sane
[formatters]
keys=sane
[logger_root]
level=DEBUG
handlers=console
# Our custom formatter class
[formatter_sane]
class=logsna.Formatter
"""
config = StringIO(CONFIG)
logging.config.fileConfig(config)
log = logging.getLogger('mylogger.component1')
log.debug('debug message')
log.info('info message')
log.warning('warning message')
log.critical('critical message')
try:
1 / 0
except:
log.exception('Houston we have a problem')
这是在代码中直接使用它的方法
import logging
import logsna
# create logger
log = logging.getLogger('mylogger.component1')
log.setLevel(logging.DEBUG)
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create an instance of the sane formatter
formatter = logsna.Formatter()
# add our formatter to the console handler
ch.setFormatter(formatter)
# add the console handler to the logger
log.addHandler(ch)
log.debug('debug message')
log.info('info message')
log.warning('warning message')
log.critical('critical message')
try:
1 / 0
except:
log.exception('Houston we have a problem')
日志格式
以下是上述程序的输出
DEBUG [2012-05-21 01:59:23,686] mylogger.component1: debug message INFO [2012-05-21 01:59:23,686] mylogger.component1: info message WARNING [2012-05-21 01:59:23,686] mylogger.component1: warning message CRITICAL [2012-05-21 01:59:23,686] mylogger.component1: critical message ERROR [2012-05-21 01:59:23,686] mylogger.component1: Houston we have a problem ! Traceback (most recent call last): ! File "/home/alienoid/python/sanefmt.py", line 67, in <module> ! 1 / 0 ! ZeroDivisionError: integer division or modulo by zero
日志格式目标
尽可能地可读
使其易于与标准Unix工具 tail 和 grep 一起使用,以帮助快速找出事情出错的原因
日志格式说明
所有时间戳都使用 ISO8601 和 UTC 格式
为了搜索特定级别的消息
$ tail -f sanefmt.log | grep '^INFO'
为了搜索特定记录器的消息
$ tail -f sanefmt.log | grep 'component1:'
为了提取带有相应日志消息的完整异常跟踪
$ tail -f sanefmt.log | grep -B 1 '^\!'
上述命令的输出将如下所示
ERROR [2012-05-21 01:59:23,686] mylogger.component1: Houston we have a problem ! Traceback (most recent call last): ! File "fmttest.py", line 72, in <module> ! 1 / 0 ! ZeroDivisionError: integer division or modulo by zero
增强日志格式输出
以下是 Logsna 格式化器使用的格式字符串
'%(levelname)-8s [%(asctime)s] %(name)s: %(message)s'
您可以在配置文件中使用 format 指令明确指定它
# Our custom formatter class [formatter_sane] format=%(levelname)-8s [%(asctime)s] %(name)s: %(message)s class=logsna.Formatter
您还可以通过添加自定义属性来增强格式字符串。有关预定义的日志记录属性集合,请参阅此处
致谢
许可证
版权(c)2012 Ruslan Spivak
在MIT许可证下发布,请参阅LICENSE
变更历史
1.2 (2012-10-02)
Python 2.6.x 兼容性
1.1 (2012-05-21)
setup.py 中的错误: https://github.com/rspivak/logsna/pull/1
1.0 (2012-05-21)
公开发布
项目详情
关闭
logsna-1.2.zip 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | c6ce37cebafcc2e1f3f25c89f1c64c07106e96b264e860ff9a9dbfe5933236fa |
|
| MD5 | d08a764505ae309070b721d1cad754da |
|
| BLAKE2b-256 | 084b16a8fe062b0c2520510bea6be2a1d165bc3ee2105cf945884b2834b551c1 |