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:自动记录类中每个方法的名称和输出_Python_Logging - Fatal编程技术网

Python:自动记录类中每个方法的名称和输出

Python:自动记录类中每个方法的名称和输出,python,logging,Python,Logging,我有一个模块,它是C程序的包装类 每个方法都有不同的输入,但输出通常是相同的,即文件名 class Wrapper(object): def wrapper1(self, infile, outfile): do_stuff log.info('wrapper1:%s' %outfile) 在我意识到我想要这种行为之前,我已经写了50个方法 另外,必须有一种优雅的方式来添加一个记录器,而不是将该行添加到所有方法中 干杯使用: 别忘了将infle/ou

我有一个模块,它是C程序的包装类

每个方法都有不同的输入,但输出通常是相同的,即文件名

class Wrapper(object): 
    def wrapper1(self, infile, outfile):
        do_stuff

        log.info('wrapper1:%s' %outfile)
在我意识到我想要这种行为之前,我已经写了50个方法

另外,必须有一种优雅的方式来添加一个记录器,而不是将该行添加到所有方法中

干杯

使用:


别忘了将infle/outfile转发到
func
我必须研究这个答案,谢谢!应该有一种方法可以自动获得正确的方法名称?好的…我想它可以工作了…我还不明白所有的内容…包装器。uu name_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。
import functools
def log(func):
    @functools.wraps(func)
    def wrapper(self, infile, outfile):
        retval = func(self, infile, outfile)
        log.info('wrapper1:%s' %outfile)
        return retval
    return wrapper

class Wrapper(object):
    @log
    def wrapper1(self, infile, outfile):
        do_stuff