Alv*_*olm 7 python oop decorator flask uwsgi
我正在尝试使用装饰器函数记录错误app.logger.error(''),但它只是不起作用.另外我不能很好地调试这个,我只能看到来自http客户端的响应:
(我正在使用nginx + uwsgi + flask)
HTTP/1.1 502 Bad Gateway
服务器:nginx
日期:太阳,2012年8月12日15:45:09 GMT
内容类型:text/html
内容长度:14
连接:保持活力
一切都行得很好: app.logger.error('panic !!!')
def mydecorator():
def decorator(f):
def wrapped_function(*args, **kwargs):
try:
ip = Mytable.query.filter_by(ip=request.remote_addr).first()
except:
app.logger.error('panic !!!')
else:
dootherthing()
resp = make_response(f(*args, **kwargs))
h = resp.headers
h['add-this-header'] = ":)"
return resp
return update_wrapper(wrapped_function, f)
return decorator
Run Code Online (Sandbox Code Playgroud)
它似乎是脱离背景或某种东西.
事实上,装饰器无法在上下文中检测到app实例,我使用current_app解决了这个问题:
1.导入方法:from flask import current_app
第2位.将任何app类附加到current_app: current_app.logger.error('panic !!!')
info @ http://flask.pocoo.org/docs/api/#flask.current_app
"指向处理请求的应用程序.这对于希望支持并行运行的多个应用程序的扩展非常有用.这由应用程序上下文而不是请求上下文提供支持,因此您可以通过使用来更改此代理的值app_context()方法."