Bulb Registration Errors

I am trying to register an error in a decorator function using app.logger.error('') , but that just doesn't work. In addition, I cannot debug this situation, and I can only see the response from the http client:

(I use nginx + uwsgi + flask)

HTTP / 1.1 502 Bad Gateway

Server: nginx

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

Content-Type: text / html

Content-Length: 14

Connection: keep-alive

Everything works fine with the line: 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 to be out of context or something like that.

+7
source share
2 answers

in fact, the decorator was unable to detect the application instance out of context, I solve this with current_app:

first. Import method: from flask import current_app

second. add any application class to current_app: current_app.logger.error('panic !!!')

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

"Indicates the application processing the request. This is useful for extensions that want to support multiple applications running on the side. It depends on the application context and not on the request, so you can change the value of this proxy using the app_context ( )

+9
source

Is the app defined anywhere in the script you published?

In addition, to help with debugging, you should consider the run() method with debug mode when testing.

 app.run(debug=True) 
0
source

All Articles