在我的 Python 3.7.1 应用程序中,我使用 Flask 并将其部署到 Heroku,除了登录到 Heroku 控制台之外,一切正常。我搜索了答案,并认为我找到了一些......但可惜它们最终没有打印到 Heroku 日志控制台。当我在本地运行该应用程序时,使用“py app.py”它显示正常。
在我的 Procfile 中,我有以下内容:
web: gunicorn app:app --log-level debug --log-file=-
在我的 app.py 文件中,我有以下内容:
if __name__ == '__main__':
formatter = logging.Formatter( "%(asctime)s | %(pathname)s:%(lineno)d | %(funcName)s | %(levelname)s | %(message)s ")
handler = RotatingFileHandler('logs/SlackBotApp.log', maxBytes=10000, backupCount=5)
handler.setLevel(logging.DEBUG)
handler.setFormatter(formatter)
app.logger.addHandler(handler)
app.logger.addHandler(logging.StreamHandler(stream=sys.stdout))
app.logger.setLevel(logging.DEBUG)
app.run()
我像这样调用记录器:
app.logger.info("Url Requested: {}".format(url))
我感觉我需要修改我的 Procfile,但我不确定做什么。有人可以提出建议吗?
最佳答案
我通过将以下内容添加到我的 app.py 中解决了这个问题:
if __name__ != '__main__':
gunicorn_logger = logging.getLogger('gunicorn.error')
app.logger.handlers = gunicorn_logger.handlers
app.logger.setLevel(gunicorn_logger.level)
并将我的 Procfile 调整为:
web: gunicorn app:app --log-level=debug
感谢https://medium.com/@trstringer/logging-flask-and-gunicorn-the-manageable-way-2e6f0b8beb2f寻找答案
关于python - 如何使用 Flask 在 Heroku 中显示标准输出日志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54297215/