跳转到主要内容

一个提供合理日志输出格式的Python小库

项目描述

 _    ___   ___ ___ _  _   _
| |  / _ \ / __/ __| \| | /_\
| |_| (_) | (_ \__ \ .` |/ _ \
|____\___/ \___|___/_|\_/_/ \_\

logsna 是一个提供合理日志输出格式的Python小库。

http://logsna.readthedocs.org

安装

$ [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

日志格式目标

  1. 尽可能地可读

  2. 使其易于与标准Unix工具 tailgrep 一起使用,以帮助快速找出事情出错的原因

日志格式说明

  1. 所有时间戳都使用 ISO8601UTC 格式

  2. 为了搜索特定级别的消息

    $ tail -f sanefmt.log | grep '^INFO'
  3. 为了搜索特定记录器的消息

    $ tail -f sanefmt.log | grep 'component1:'
  4. 为了提取带有相应日志消息的完整异常跟踪

    $ 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)

1.0 (2012-05-21)

  • 公开发布

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源分布

logsna-1.2.zip (10.2 kB 查看哈希值)

上传时间

由以下支持