使用flas记录错误

2024-04-28 22:53:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用app.logger.error('')在decorator函数中记录一个错误,但它根本不起作用。此外,我无法很好地调试,只能看到来自http客户端的响应:

(我用的是nginx+uwsgi+烧瓶)

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

似乎是断章取义之类的。


Tags: ipappreturndefargsnginxfunctiondecorator
2条回答

是否在您发布的脚本中的任何地方定义了app

另外,为了帮助调试,在测试时应该考虑对debug mode使用run()方法。

app.run(debug=True)

实际上,decorator无法检测上下文外的应用程序实例,我使用当前的应用程序解决这个问题:

1.导入方法:from flask import 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."

相关问题 更多 >