无角角角
Log的Python项目详细描述
对数
python的非常规日志记录
Author: Michal Hordecki URL: http://github.com/MHordecki/LogPy
简介
logpy是标准python日志工具的一种替代方法,松散地 基于Lisp的log5。洛比是基于亲吻原则的-所以我想要 它要尽可能的透明。
与stdlib的日志相比,主要的区别在于基于标记的体系结构。 在日志记录中,每个日志都分配了一定的级别(调试、错误等)。 就这些。洛比,另一方面,运动标签-你可以附加短字符串 每一条信息。标记可以表示多种情况:严重性级别、模块 名称或某些自定义日志分类。
logpy需要python 2.6或更高版本。它也可以在python 3上无缝工作 (实际上,它是在考虑py3k的情况下开发的,然后被移植到python 2.6)。
开始
使用logpy非常简单:
from logpy import LogPy import sys log = LogPy() log.add_output(sys.stderr.write) log('debug')('Hello World!')
喂!logpy实例是可调用的。要输出日志,请调用log“两次”-in 第一个调用传递日志的所有标记,所有内容都传递给第二个调用 将被视为信息的一部分。该示例将日志输出到 标准错误输出。很容易,不是吗?
引擎盖下
logpy有几个抽象层:
- LogPy - it accepts data from the user, combines them into a Message instance and passes them down to all outputs.
- Output - it filters messages based on some predefined conditions, and if the message passes them all, it’s formatted by the Formatter and then passed to the actual output.
- Formatter - takes message and formats it ;) (in standard implementation it uses string.format for the job).
- Actual output - a callable that, for example, outputs the Formatter’s output to the screen.
所有这些层/对象都是可调用的。
常见任务
输出滤波
对于多个输出,您可能希望筛选出每个输出中的一些日志。有人对此表示支持:
log = LogPy() log.add_output(my_output, filter = lambda m: 'error' in m.tags) # Equivalent to: log.add_output(my_output, filter = [lambda m: 'error' in m.tags])
如您所见,过滤器是可调用的,将Messageobject作为参数 并返回bool。一个列表可以提供多个筛选器。
自定义格式
您可以通过替换格式字符串或完全替换格式对象来自定义格式。你的选择。
自定义格式字符串
这个能满足你90%的需求。您可以将带关键字参数的格式字符串更改为logpy的add_output方法(也可以在直接实例化输出对象时):
log.add_output(..., formatter = 'my custom format string!')
处理消息时,字符串的方法format将使用 以下是预定义参数:
- date - datetime object
- tags - space-delimited list of tags (string)
- args - list of arguments in the message
- kwargs - dict of keyword arguments in the message
- message - the actual message object. All arguments above are actually just a syntactic sugar, as they are all attributes of this object.
默认格式字符串如下:{date} : {tags} : {args} {kwargs}\n
别忘了在结尾处加一个新行,否则你的日志会显得残废。
使用多个模块
在对多个模块使用logpy时,可以通过预定义 一些标签:
# Main module log = LogPy() # Child module import mainmodule log = mainmodule.log('module:childmodule', curry = True) # Now: log('debug')('Hello World!') # is equivalent to log('module:childmodule', 'debug')('Hello World')
自定义格式对象
如果您需要全功率,可以去掉默认格式化程序:
log.add_output(..., formatter = my_formatter_object)
格式化程序对象必须符合简单协议:
class Formatter: def __call__(message: Message) -> Someting reasonable: pass class Message: tags = set(str) args = [] # passed by the user kwargs = {} # passed by the user date = datetime.datetime
(我不知道是否有标准的形式符号来描述 除了像zope.interface这样的东西之外,python中的协议。我希望我的漫无目的 已清除。)
合理的东西意味着:所有被接受的东西 输出(听起来有点傻)-通常是指 str,但并不总是。
自定义输出对象
如果您愿意放弃Logpy 50%的loc,请随时这样做:
log.add_raw_output(my_customized_output_object)
值得一提的是,LogPy.add_output只是一个包装:
log.add_output(...) # Equivalent to log.add_raw_output(Output(...))
输出协议如下:
class Output: def __call__(message: Message): pass
换句话说:用户发出的每个日志都将调用您。
注意:请将消息视为不可变对象-它们正在被重用 所有输出。
螺纹安全性
logpy使用了一些基本的线程安全性;logpy的call方法中使用了threading.lock。它很容易更换:
from threading import RLock log = LogPy() log.lock = RLock()