用于对库方法调用进行负责任的低模板日志记录的工具包
logfur的Python项目详细描述
logfury是针对python库维护人员的。它允许对方法调用进行负责任的低模板日志记录。
许可证
BSD 3条款
奇怪的导入是什么
fromlogfury.v0_1importDefaultTraceMeta
如果要在库中使用logfury,对api的任何更改都可能会破坏程序。没人想要。
由于这个导入技巧,我可以保持0.1.x api非常稳定。同时,我可以更改库的功能并更改0.2.x版等的默认行为,而不必更改包的名称。这样,您就可以决定何时采用可能不兼容的api更改,方法是在导入时递增api版本。
安装
电流稳定
pip install logfury
开发版本
git clone git@github.com:ppolewicz/logfury.git python setup.py install
基本用法
DefaultTraceMeta元类
>>> importlogging>>> importsix>>> >>> fromlogfury.v0_1importDefaultTraceMeta,limit_trace_arguments,disable_trace>>> >>> >>> logging.basicConfig()>>> logger=logging.getLogger(__name__)>>> logger.setLevel(logging.DEBUG)>>> >>> >>> @six.add_metaclass(DefaultTraceMeta)>>> classFoo(object):... defbaz(self,a,b,c=None):... returnTrue... defget_blah(self):... return5... def_hello(self):... pass... @disable_trace... defworld(self):... pass... def__repr__(self):... return'<%s object>'%(self.__class__.__name__,)...>>> classBar(Foo):... defbaz(self,a,b,c=None):... b+=1... returnsuper(Bar,self).baz(a,b,c)... defworld(self):... pass... @limit_trace_arguments(skip=['password'])... defsecret(self,password,other):... pass... @limit_trace_arguments(only=['other'])... defsecret2(self,password,other):... pass...>>> a=Foo()>>> a.baz(1,2,3)DEBUG:__main__:calling Foo.baz(self=<Foo object>, a=1, b=2, c=3) >>> a.baz(4,b=8)DEBUG:__main__:calling Foo.baz(self=<Foo object>, a=4, b=8) >>> a.get_blah()# nothing happens, since v0_1.DefaultTraceMeta does not trace "get_.*">>> a._hello()# nothing happens, since v0_1.DefaultTraceMeta does not trace "_.*">>> a.world()# nothing happens, since v0_1.DefaultTraceMeta does not trace "_.*">>> b=Bar()>>> b.baz(4,b=8)# tracing is inheritedDEBUG:__main__:calling Bar.baz(self=<Bar object>, a=4, b=8) DEBUG:__main__:calling Foo.baz(self=<Bar object>, a=4, b=9, c=None) >>> b.world()# nothing happens, since Foo.world() tracing was disabled and Bar inherited it>>> b.secret('correct horse battery staple','Hello world!')DEBUG:__main__:calling Bar.secret(self=<Bar object>, other='Hello world!') (hidden args: password) >>> b.secret2('correct horse battery staple','Hello world!')DEBUG:__main__:calling Bar.secret2(other='Hello world!') (hidden args: self, password)
跟踪调用decorator
>>> importlogging>>> fromlogfuryimport*>>> logging.basicConfig()>>> logger=logging.getLogger(__name__)>>> >>> @trace_call(logger)... deffoo(a,b,c=None):... returnTrue...>>> foo(1,2,3)True >>> logger.setLevel(logging.DEBUG)>>> foo(1,2,3)DEBUG:__main__:calling foo(a=1, b=2, c=3) True >>> foo(1,b=2)DEBUG:__main__:calling foo(a=1, b=2) True >>>