How to see Heroku log output and runtime result while python is running

I am working on a Python web application with Heroku and I cannot figure out how to test it effectively. I tried to use print (...) and sys.stdout.write (...), but I never see any output when I run locally with the “start of the wizard” or when I deploy in the cloud and run the “hero logs” to see cloud magazines. Also, I cannot figure out how to debug python runtime errors, for example when an exception is thrown. The web request for the application returns HTTP 500, but I have no way of debugging to see where the exception came from. Is there any way to see this information?

+11
source share
3 answers

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

In my case, the problem was that stdout is buffered, but stderr is not.

If you don’t even see an exception through foreman start , make sure that you are actually clicking on your server on the correct IP / PORT. You should see HTTP access entries (e.g. GET / index.html) at the exit from foreman start .

In addition, you can try using the web framework to work in debug mode - most modern frameworks will display stacks in the browser in debug / dev mode. Using a 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() 
+15
source

If you use Foreman to run a Python project and you are unable to see stdout / stderr, here are a few solutions for you. If you use Procfile to directly call the python CLI, you can use the '-u' option to avoid stdout buffering:

 python -u script.py 

If you use Procfile to manage a WSGI server, for example by calling gunicorn, a jar, a bottle, eve, etc., you can add the ".env" file to the root of your python project containing the following

 PYTHONUNBUFFERED=True 

Also see the answers to this question: the wizard only shows a line with "start wit pid #" and nothing else

+13
source

Note to any other Django / Python beginners. Do not forget to add

 import sys 

and something like this

 print ("hello world") # python 3 sys.stdout.flush() 

prints to the console.

Switching from PHP to Py / Dj KISS can be rare.

+7
source

All Articles