3

I use next code to caught uncaught exceptions in my Flask app:

@app.errorhandler(Exception)
def uncaught_exception_handler(error):
    if not app.config['DEVELOPMENT']:
        app.logger.error(error, exc_info=True)
    else:
        raise error

    return 'Internal Server Error', 500

How can I add extra information about values of variables in each stack frame to my log file? It looks like cgitb is doing desired things but it print error info to stdout or to a file and not returned it as a string. I know that it is possible to do what I want with inspect and traceback modules but as this is Python I think that there already exists appropriate solution for such problem.

Ivan Velichko
  • 6,348
  • 6
  • 44
  • 90
  • You can dump the `locals()` or `globals()` should you need to. – Anzel Jan 19 '15 at 10:41
  • And what about not current stack frame? Should I call locals() for every frame manually? – Ivan Velichko Jan 19 '15 at 10:51
  • what do you mean by "not current"? what are your expected **variables** you want to capture? – Anzel Jan 19 '15 at 11:04
  • When exception is raised (exactly at that moment) program has call stack with depth n. Current frame for me is that frame at which exception is raised. Frame of caller (function that called function that raised an exception) has own stack frame too (depth n-1). And so on. And I want to obtain values of each local variable within every frames. Exactly like cgitb doing that in [this example](http://pymotw.com/2/cgitb/). – Ivan Velichko Jan 19 '15 at 11:12
  • 1
    Alright I see what you mean, basically you want the full stack, you may be interested in this [SO Answer](http://stackoverflow.com/questions/13210436/get-full-traceback) – Anzel Jan 19 '15 at 11:26

1 Answers1