gpt4 book ai didi

python - 用 flask 记录错误

转载 作者:太空狗 更新时间:2023-10-29 21:37:53 24 4
gpt4 key购买 nike

我正在尝试使用 app.logger.error('') 在装饰器函数中记录错误,但它不起作用。另外我不能很好地调试它,我只能看到来自 http 客户端的响应:

(我用的是nginx+uwsgi+flask)

HTTP/1.1 502 Bad Gateway

Server: nginx

Date: Sun, 12 Aug 2012 15:45:09 GMT

Content-Type: text/html

Content-Length: 14

Connection: keep-alive

一切都很好,没有一行: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

好像是断章取意什么的

最佳答案

事实上,装饰器无法检测出上下文之外的应用程序实例,我使用 current_app 解决了这个问题:

第一。导入方法:from flask import current_app

第二。将任何应用类附加到 current_app:current_app.logger.error('panic !!!')

info @ http://flask.pocoo.org/docs/api/#flask.current_app

"Points to the application handling the request. This is useful for extensions that want to support multiple applications running side by side. This is powered by the application context and not by the request context, so you can change the value of this proxy by using the app_context() method."

关于python - 用 flask 记录错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11925558/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com