简单日志分析库
loggrok的Python项目详细描述
- 我以前写过loggrok,因为我需要一个简单的库
用于分析日志。我没有积极发展洛格洛克,但请让我知道 如果您发现任何错误等。
功能包括:
- 简单回调系统(loggrok.actions)
- 多个基于滚动索引的日志文件上的seemless迭代-智能连接 虚线(RollingIndexLogstream)
- 基于正则表达式的自定义头和消息体匹配。
示例用法:
>>> from loggrok.actions import Action >>> action = Action() >>> def printError(entry): ... print 'err!', str(entry)[:-1] ... >>> action.addLevelCallback('ERROR', printError) >>> def printWarning(entry): ... print 'warning!', str(entry)[:-1] ... >>> action.addLevelCallback('WARN', printWarning) >>> from loggrok.log import LogStream >>> stream = LogStream(fname) >>> stream.action = action >>> for entry in stream: ... continue ... err! blah blah warning! blah blah err! blah blah
您还可以编写自己的正则表达式来匹配自定义头:
>>> from loggrok.parse import HeaderParser, MessageParser >>> header_patt = r'^([a-zA-Z]+) ([a-zA-Z]+) <(\d+)> ' # Entry attributes correspond to groups in regex pattern >>> header_attrs = ('foo', 'bar', 'baz') >>> header_parser = HeaderParser(header_patt, header_attrs) >>> message_patterns = (...) # regexs for message body - after header >>> message_attrs = (...) # tuple of attribute tuples corresponding to patters >>> messageParser = MessageParser(message_patterns, message_attrs) ... >>> stream.messageParser = messageParser
有关工作示例,请参见tests目录中的doctest。
运行单元测试:
python runtests.py
注意:loggrok将发出与“breaked”curriedcallable类相关的警告 它不应该引起问题。