简体   繁体   English

用烧瓶记录错误

[英]logging errors with flask

I'm trying to log an error in a decorator function using app.logger.error('') , but it just doesn't work. 我正在尝试使用app.logger.error('')在装饰器函数中记录错误,但它只是不起作用。 In addition I cant debug this well and I can only see the response from the http client: 另外我不能很好地调试这个,我只能看到来自http客户端的响应:

(I'm using nginx+uwsgi+flask) (我正在使用nginx + uwsgi + flask)

HTTP/1.1 502 Bad Gateway HTTP / 1.1 502 Bad Gateway

Server: nginx 服务器:nginx

Date: Sun, 12 Aug 2012 15:45:09 GMT 日期:太阳,2012年8月12日15:45:09 GMT

Content-Type: text/html 内容类型:text / html

Content-Length: 14 内容长度:14

Connection: keep-alive 连接:保持活力

Everything works great with out the line: app.logger.error('panic !!!') 一切都运行得很好: 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

It seems that it is out of context or something. 它似乎是脱离背景或某种东西。

in fact, the decorator wasnt able to detect the app instance out of context, i solve this using current_app: 事实上,装饰器无法在上下文中检测到app实例,我使用current_app解决了这个问题:

1st. 1。 Import the method: from flask import current_app 导入方法: from flask import current_app

2nd. 第2位。 append any app class to current_app: current_app.logger.error('panic !!!') 将任何app类附加到current_app: current_app.logger.error('panic !!!')

info @ http://flask.pocoo.org/docs/api/#flask.current_app 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." “指向处理请求的应用程序。这对于希望支持并行运行的多个应用程序的扩展非常有用。这由应用程序上下文而不是请求上下文提供支持,因此您可以通过使用来更改此代理的值app_context()方法。“

Is app defined anywhere in the script that you've posted? app是否已在您发布的脚本中的任何位置定义?

Also, to help with debugging, when testing you should consider using the run() method with debug mode . 另外,为了帮助调试,在测试时应考虑将run()方法与调试模式一起使用

app.run(debug=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM