如何组合装饰器?

时间:2018-02-01 15:19:50

标签: python-3.x decorator functools

我希望记录器打印调用subprocess.run方法的亚麻,下面的例子打印定义了wrapfunction的亚麻布

#!/usr/bin/env python3
import logging, sys, functools, subprocess    

logging.basicConfig(stream=sys.stdout, level=logging.INFO,style='{',format='⬬ {lineno:4d} ⬤ {pathname} ⬮\n{msg}')    

def wrap_function(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        logging.info((args,kwargs))  # lineno=9
        return func(*args ,**kwargs)
    return wrapper    

subprocess.run = wrap_function(subprocess.run)    

subprocess.run('ls',timeout=10,shell=True)  # lineno=15

当然我得到了调用logging.info的行号:

# current output:
#⬬    9 ⬤ ./PycharmProjects/02.py ⬮
#(('ls',), {'timeout': 10, 'shell': True})

有没有办法在不调用logging.info的情况下为logging.info定义msg参数? 这是我想要的输出:

# output I would like to have:
#⬬   15 ⬤ ./PycharmProjects/02.py ⬮
#(('ls',), {'timeout': 10, 'shell': True})

0 个答案:

没有答案