Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Monkeypatching logging.Logger——参数是如何工作的?_Python_Logging_Monkeypatching - Fatal编程技术网

Python Monkeypatching logging.Logger——参数是如何工作的?

Python Monkeypatching logging.Logger——参数是如何工作的?,python,logging,monkeypatching,Python,Logging,Monkeypatching,我有一个我编写的日志类,我喜欢使用它。看起来是这样的: class EasyLogger(object): SEP = " " def __init__(self, logger=logging.getLogger(__name__)): self.logger = logger def _format_str(self, *args): return self.SEP.join([str(a) for a in args])

我有一个我编写的日志类,我喜欢使用它。看起来是这样的:

class EasyLogger(object):
    SEP = " "

    def __init__(self, logger=logging.getLogger(__name__)):
        self.logger = logger

    def _format_str(self, *args):
        return self.SEP.join([str(a) for a in args])

    def debug(self, *args):
        self.logger.debug(self._format_str(*args))

    .... repeated for the other logging methods, like info, warning, etc....

    def __getattr__(self, name):
        return getattr(self.logger, name)
def __init__(self, logger=logging.getLogger(__name__)):
    self.logger = logger
    # Ugly, ugly, ugly dirty hack to fix line numbers
    self.logger.findCaller = find_caller_monkeypatch
这给了我如下语法:

LOG.debug("some_variable: ", some_variable)
我喜欢。我不想从
logging.Logger
继承,因为组合比继承好,而且我真的不想把内置类弄得一团糟

我的格式字符串如下所示:

LOGGING_FMT = "<%(filename)s:%(lineno)s(%(levelname)s) - %(funcName)s() >"\
                        "%(message)s"
这是可行的,但我很困惑。我最初使用以下签名定义了
find\u caller\u monkeypatch

def find_caller_monkeypatch(self):
(函数体是从链接的答案复制+粘贴的)。但是,这会导致一个错误:

  File "/usr/lib64/python2.7/logging/__init__.py", line 1262, in _log
    fn, lno, func = self.findCaller()
TypeError: find_caller_monkeypatch() takes exactly 1 argument (0 given)
删除
self
作为
find\u caller\u multipatch
的参数可以修复错误,但我感到困惑的是:
find\u caller\u monkeypatch
为什么不将
self
作为参数调用为
self.findCaller()
?如果方法是在类内部定义的,那么方法是否仅将
self
作为参数获取

以下代码段是同一问题的较小示例:

In [7]: class Foo(object):
   ...:     def say_hi(self):
   ...:         print "Hello World"
   ...:         

In [8]: def say_bye_self(self): print "Goodbye world"

In [9]: def say_bye(): print "Goodbye world"

In [10]: foo = Foo()

In [11]: foo.say_hi()
Hello World

In [12]: foo.say_hi = say_bye_self

In [13]: foo.say_hi()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-10baea358e0b> in <module>()
----> 1 foo.say_hi()

TypeError: say_bye_self() takes exactly 1 argument (0 given)

In [14]: foo.say_hi = say_bye

In [15]: foo.say_hi()
Goodbye world
[7]中的
:类Foo(对象):
…:def say_hi(self):
…:打印“Hello World”
...:         
在[8]:def say_bye_self(self):打印“再见世界”
在[9]:def say_bye():打印“再见世界”
在[10]中:foo=foo()
在[11]中:foo.say__hi()
你好,世界
在[12]中:foo.say\u hi=说再见
在[13]中:foo.say__hi()
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
---->1 foo.say__hi()
TypeError:say_bye_self()正好接受1个参数(给定0个)
在[14]中:foo.say\u hi=说再见
在[15]中:foo.say__hi()
再见世界

这是怎么回事?

你可以在课堂上用Monkeypatch

Foo.say_hi = say_bye_self
foo = Foo()
foo.say_hi()
或者,您可以对实例进行Monkeypatch

import types
foo.say_hi = types.MethodType(say_bye_self, foo)

有趣。但是为什么monkeypatched函数没有将
self
作为参数?我认为
object.function()
总是将
object
作为参数传递给
function