2
我有一个由许多其他类继承的类。相关类有记录方法和记录文字旁边一些额外的参数,我希望记录:集中日志记录方法和日志记录当前行号
import logging
# some lines to set logger
log = logging.getLogger('some_loger')
class SomeClass:
def log_it(self, log_text):
log.info(self.some_param + self.some_other_param + log_text + self.some_another_param)
而且在继承SomeClass
的任何文件:
class MyOtherClass(SomeClass):
def some_method(self):
...
...
self.log_it('let me log this text')
我也希望记录当前行号(调用self.log_it
的行),因此我可以轻松找到相关文件中的当前记录行。
什么是最好的pythonic方式来做到这一点?
如果您不熟悉这个概念,您可能会对[AOP](https://en.wikipedia.org/wiki/Aspect-oriented_programming)感兴趣,并且不直接回答您的问题。这种编程风格通常以记录用例为例。如果你想阅读更多+ Python支持,请参阅这篇文章http://stackoverflow.com/questions/12356713/aspect-oriented-programming-aop-in-python。概念验证:https://bitbucket.org/jsbueno/metapython/src/f48d6bd388fd/aspect.py – 2013-07-24 12:44:49