Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to see Heroku logging and runtime error output in python

I'm working on a web app in Python with Heroku and I can't figure out how to effectively test debug it. I tried using print(...) and sys.stdout.write(...) but I never see any output when I run locally with 'foreman start' or when I deploy to the cloud and run 'heroku logs' to see cloud logs. Furthermore, I can't figure out how to debug python runtime errors such as when an exception occurs. The web request for the app returns an HTTP 500 but I have no way of debugging that to see where the exception originated. Is there any way to see this information?

like image 719
broseflip Avatar asked Sep 20 '12 00:09

broseflip


1 Answers

Adding sys.stdout.flush() after print statements solved this problem for me.

In my case, the problem seemed to be that stdout is buffered whereas stderr is not.

If you're not even seeing exceptions via foreman start, make sure you're actually hitting your server at the right IP/PORT. You should see HTTP access entries (e.g., GET /index.html) in the output from foreman start.

Also, you might try telling your web framework to run in debug mode—most modern frameworks will show stacktraces in the browser in debug/dev mode. Using Flask? app.config['DEBUG'] = True or app.run(..., debug=True).

# logging helper
def p(*args):
  print args[0] % (len(args) > 1 and args[1:] or [])
  sys.stdout.flush()
like image 75
cameronboehmer Avatar answered Sep 20 '22 02:09

cameronboehmer