logging.error()调用了多少次?

时间:2022-02-08 02:14:52

Maybe it's just doesn't exist, as I cannot find it in pydoc. But using python's logging package, is there a way to query a Logger to find out how many times a particular function was called? For example, how many errors/warnings were reported?

也许它只是不存在,因为我在pydoc中找不到它。但是使用python的日志包,有没有办法查询Logger以找出调用特定函数的次数?例如,报告了多少错误/警告?

3 个解决方案

#1


The logging module doesn't appear to support this. In the long run you'd probably be better off creating a new module, and adding this feature via sub-classing the items in the existing logging module to add the features you need, but you could also achieve this behavior pretty easily with a decorator:

日志记录模块似乎不支持此功能。从长远来看,您可能最好创建一个新模块,并通过对现有日志记录模块中的项进行子类化来添加此功能以添加所需的功能,但您也可以使用装饰器轻松实现此行为:

class CallCounted:
    """Decorator to determine number of calls for a method"""

    def __init__(self,method):
        self.method=method
        self.counter=0

    def __call__(self,*args,**kwargs):
        self.counter+=1
        return self.method(*args,**kwargs)


import logging
logging.error = CallCounted(logging.error)
logging.error('one')
logging.error('two')
print(logging.error.counter)

Output:

ERROR:root:one
ERROR:root:two
2

#2


You can also add a new Handler to the logger which counts all calls:

您还可以向记录器添加新的处理程序,以计算所有呼叫:

class MsgCounterHandler(logging.Handler):
    level2count = None

    def __init__(self, *args, **kwargs):
        super(MsgCounterHandler, self).__init__(*args, **kwargs)
        self.level2count = {}

    def emit(self, record):
        l = record.levelname
        if (l not in self.level2count):
            self.level2count[l] = 0
        self.level2count[l] += 1

You can then use the dict afterwards to output the number of calls.

然后,您可以使用dict输出呼叫数。

#3


There'a a warnings module that -- to an extent -- does some of that.

有一个警告模块 - 在某种程度上 - 做了一些。

You might want to add this counting feature to a customized Handler. The problem is that there are a million handlers and you might want to add it to more than one kind.

您可能希望将此计数功能添加到自定义处理程序。问题是有一百万个处理程序,您可能希望将其添加到多种处理程序中。

You might want to add it to a Filter, since that's independent of the Handlers in use.

您可能希望将其添加到Filter,因为它独立于正在使用的处理程序。

#1


The logging module doesn't appear to support this. In the long run you'd probably be better off creating a new module, and adding this feature via sub-classing the items in the existing logging module to add the features you need, but you could also achieve this behavior pretty easily with a decorator:

日志记录模块似乎不支持此功能。从长远来看,您可能最好创建一个新模块,并通过对现有日志记录模块中的项进行子类化来添加此功能以添加所需的功能,但您也可以使用装饰器轻松实现此行为:

class CallCounted:
    """Decorator to determine number of calls for a method"""

    def __init__(self,method):
        self.method=method
        self.counter=0

    def __call__(self,*args,**kwargs):
        self.counter+=1
        return self.method(*args,**kwargs)


import logging
logging.error = CallCounted(logging.error)
logging.error('one')
logging.error('two')
print(logging.error.counter)

Output:

ERROR:root:one
ERROR:root:two
2

#2


You can also add a new Handler to the logger which counts all calls:

您还可以向记录器添加新的处理程序,以计算所有呼叫:

class MsgCounterHandler(logging.Handler):
    level2count = None

    def __init__(self, *args, **kwargs):
        super(MsgCounterHandler, self).__init__(*args, **kwargs)
        self.level2count = {}

    def emit(self, record):
        l = record.levelname
        if (l not in self.level2count):
            self.level2count[l] = 0
        self.level2count[l] += 1

You can then use the dict afterwards to output the number of calls.

然后,您可以使用dict输出呼叫数。

#3


There'a a warnings module that -- to an extent -- does some of that.

有一个警告模块 - 在某种程度上 - 做了一些。

You might want to add this counting feature to a customized Handler. The problem is that there are a million handlers and you might want to add it to more than one kind.

您可能希望将此计数功能添加到自定义处理程序。问题是有一百万个处理程序,您可能希望将其添加到多种处理程序中。

You might want to add it to a Filter, since that's independent of the Handlers in use.

您可能希望将其添加到Filter,因为它独立于正在使用的处理程序。