简体   繁体   English

如何使用Flask和foreman(heroku)记录错误消息?

[英]How to log error messages with Flask and foreman (heroku)?

I am working with Flask and Foreman for the Heroku hosting. 我正在与Flask和Foreman一起为Heroku托管。 I start my local server typing foreman start . 我开始我的本地服务器输入foreman start My issue is that I want to see the log error messages that my code produces but I haven't found the right way to do it. 我的问题是,我想查看我的代码生成的日志错误消息,但我没有找到正确的方法来执行此操作。

I tried with some code that I found in the Flask documentation like this, but it doesn't work either: 我尝试使用我在Flask文档中找到的一些代码,但它不起作用:

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 be able to read de error messages when running Flask with foreman start ? 知道如何在使用foreman start运行Flask时能够读取de错误消息吗?

I had a similar problem - in the end the issue wasn't with Foreman, but with using a 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 ). 我遇到了类似的问题 - 最后问题不在于Foreman,而是在使用较新版本的Gunicorn,默认情况下禁用控制台日志记录( http://gunicorn-docs.readthedocs.org/en/latest/faq .html#why-i-don-t-see-any-logs-in-the-console )。

Changing my procfile from: 更改我的procfile:

web: gunicorn app:app

to

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

solved the issue for me. 为我解决了这个问题。

Heroku and Foreman expect your logs to go to stdout or stderr , not to a file. Heroku和Foreman希望您的日志转到stdoutstderr ,而不是文件。 See the Heroku documentation on logging . 有关日志记录,请参阅Heroku文档

For a Python app you can use the logging.StreamHandler class, which logs to stderr by default. 对于Python应用程序,您可以使用logging.StreamHandler类,默认情况下会记录到stderr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM