How to log error messages using Flask and foreman (heroku)?

I work with Flask and Foreman for hosting Heroku. I start my local server by typing foreman start . My problem is that I want to see the log error messages that my code produces, but I have not found the correct way to do this.

I tried with some code that I found in the Flask documentation, but this doesn't work either:

 import logging from FileHandler import FileHandler file_handler = FileHandler("log.txt") file_handler.setLevel(logging.WARNING) app.logger.addHandler(file_handler) 

Any idea how to read error messages when starting Flask using foreman start ?

+6
source share
2 answers

I had a similar problem - in the end the problem was not with Foreman, but with the newer version of Gunicorn, where console logging was disabled by default ( http://gunicorn-docs.readthedocs.org/en/latest/ faq.html # why-i-don-t-see-any-logs-in-the-console ).

Modifying my procfile from:

 web: gunicorn app:app 

to

 web: gunicorn --log-file=- app:app 

solved the problem for me.

+8
source

Geroku and Foreman expect your logs to go to stdout or stderr , not to the file. See Heroku documentation for logging .

For a Python application, you can use the logging.StreamHandler class, which works with stderr by default.

+1
source

Source: https://habr.com/ru/post/955850/


All Articles