我想在一个模块中加入日志记录,但是我不想让该模块创建一个新的日志对象/文件,我希望它使用调用者的日志对象,无论这个对象是什么,但前提是他们必须传递一个。我知道我可以将所有的日志调用放在try块中,但那样很笨拙。最后,我想出了一种方法,似乎可以工作,但它看起来相当杂乱无章,我相信有更好的方法来解决这个问题。
class MyClass(object):
def __init__(self, arg1, arg2, log=None):
if log == None:
class log(object):
def error(self, msg): pass
def warning(self, msg): pass
def info(self, msg): pass
def debug(self, msg): pass
self.log = log()
else:
self.log = log
self.log.debug('Starting')
有什么更好的方法来做这样的事情吗?
谢谢!