【发布时间】:2014-10-21 14:30:18
【问题描述】:
我正在尝试将两个装饰器教程合并到一个装饰器中,该装饰器将在指定的日志级别记录函数参数。
第一个教程来自here,看起来像这样(并且按预期工作):
import logging
logging.basicConfig(level=logging.DEBUG)
def dump_args(func):
# get function arguments name
argnames = func.func_code.co_varnames[:func.func_code.co_argcount]
# get function name
fname = func.func_name
logger = logging.getLogger(fname)
def echo_func(*args, **kwargs):
"""
Log arguments, including name, type and value
"""
def format_arg(arg):
return '%s=%s<%s>' % (arg[0], arg[1].__class__.__name__, arg[1])
logger.debug(" args => {0}".format(', '.join(
format_arg(entry) for entry in zip(argnames, args) + kwargs.items())))
return func(*args, **kwargs)
return echo_func
第二个教程来自here。
我的组合代码如下所示并产生错误。
#decorators.py
from functools import wraps
import logging
logging.basicConfig(level=logging.DEBUG)
def logged(level=logging.INFO, name=None, message=None):
'''
Dump function arguments to log file.
Optionally, change the logging level of the call, the name of the logger to
use and the specific message to log as well
'''
def decorate(func):
# get function arguments name
argnames = func.func_code.co_varnames[:func.func_code.co_argcount]
# get function name
fname = name if name else func.__module__
logger = logging.getLogger(fname)
logmsg = message if message else None
@wraps(func)
def wrapper(*args, **kwargs):
"""
Log arguments, including name, type and value
"""
def format_arg(arg):
return '%s=%s<%s>' % (arg[0], arg[1].__class__.__name__, arg[1])
logger.log(level, " args => {0}".format(', '.join(
format_arg(entry) for entry in zip(argnames, args) + kwargs.items())))
if logmsg:
logger.log(level, logmsg)
return func(*args, **kwargs)
return wrapper
return decorate
它是从我的烧瓶应用程序中调用的,如下所示:
@app.route("/hello/")
@app.route("/hello/<name>")
@api_decorators.logged
def hello(name=None):
s = "Hello"
if name:
s = "%s %s!" % (s, name)
else:
s = "%s %s!" % (s, "World!")
return s
产生的错误是
TypeError: decorate() got an unexpected keyword argument 'name'
整个堆栈跟踪是
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\flask\app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Python27\lib\site-packages\flask\app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "C:\Python27\lib\site-packages\flask\app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Python27\lib\site-packages\flask\app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Python27\lib\site-packages\flask\app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Python27\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
TypeError: decorate() got an unexpected keyword argument 'name'
如何修复组合代码以消除此错误?
【问题讨论】:
标签: python python-2.7 flask decorator