提供正常日志输出格式的小python库
logsna的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格式
对特定级别的邮件进行grep
$ tail -f sanefmt.log | grep '^INFO'
对来自特定记录器的消息进行grep
$ tail -f sanefmt.log | grep 'component1:'
使用相应的日志消息提取完整的异常回溯
$ tail -f sanefmt.log | grep -B 1 '^\!'
The output of the above command will look like this
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'
您可以在配置文件中使用格式显式指定它 指令
# Our custom formatter class [formatter_sane] format=%(levelname)-8s [%(asctime)s] %(name)s: %(message)s class=logsna.Formatter
您还可以通过添加自定义的 如果你需要的话。 有关一组预定义的日志记录属性,请参见here
许可证
版权所有(c)2012 Ruslan Spivak
根据麻省理工学院许可证出版,请参见许可证
变更历史
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)
- 公开发布