Flask 日志记录根本不起作用

新手上路,请多包涵

我正在尝试将 Flask 中的消息记录到文件和标准输出。我一直在阅读 Flask 官方文档并得出以下结论:

 from flask import Flask
import logging
from logging import Formatter, FileHandler

app = Flask(__name__)

@app.route('/')
def hello_world():
    app.logger.debug('second test message...')
    return 'Hello World!'

if __name__ == '__main__':
    #Setup the logger
    file_handler = FileHandler('output.log')
    handler = logging.StreamHandler()
    file_handler.setLevel(logging.DEBUG)
    handler.setLevel(logging.DEBUG)
    file_handler.setFormatter(Formatter(
        '%(asctime)s %(levelname)s: %(message)s '
        '[in %(pathname)s:%(lineno)d]'
     ))
     handler.setFormatter(Formatter(
        '%(asctime)s %(levelname)s: %(message)s '
        '[in %(pathname)s:%(lineno)d]'
     ))
     app.logger.addHandler(handler)
     app.logger.addHandler(file_handler)
     app.logger.error('first test message...')
     app.run()

有几个问题:

  1. output.log 文件生成

  2. 只有第一条日志消息有效:

    app.logger.error(‘测试…’)

并且仅在标准输出中…视图“/”中的那个甚至不打印到标准输出…我做错了什么吗?

这是启动应用程序并转到 / 的输出:

 2015-03-08 11:33:27,183 ERROR: first test message... [in /home/mosquito/python_projects/flask_tst/flask_tst.py:31]
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [08/Mar/2015 11:33:43] "GET / HTTP/1.1" 200 -

原文由 AlejandroVK 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.8k
2 个回答

由于您没有在调试模式下运行,您的(调试)日志消息被 Flask 抑制了。如果将以下标志设置为 True,您的代码将起作用。

     app.run(debug=True)

这些消息现在将按预期显示。

 BennyE$ python3 stackoverflow.py
2015-03-08 12:04:04,650 ERROR: firs test message... [in stackoverflow.py:31]
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
 * Restarting with stat
2015-03-08 12:04:04,807 ERROR: firs test message... [in stackoverflow.py:31]
--------------------------------------------------------------------------------
DEBUG in stackoverflow [stackoverflow.py:11]:
second test message...
--------------------------------------------------------------------------------
2015-03-08 12:04:13,789 DEBUG: second test message... [in stackoverflow.py:11]
192.168.178.23 - - [08/Mar/2015 12:04:13] "GET / HTTP/1.1" 200 -
--------------------------------------------------------------------------------
DEBUG in stackoverflow [stackoverflow.py:11]:
second test message...
--------------------------------------------------------------------------------
2015-03-08 12:04:14,899 DEBUG: second test message... [in stackoverflow.py:11]
192.168.178.23 - - [08/Mar/2015 12:04:14] "GET / HTTP/1.1" 200 -

这是相关输出文件中的输出:

 BennyE$ cat output.log
2015-03-08 11:58:22,226 ERROR: firs test message... [in stackoverflow.py:31]
2015-03-08 12:04:04,650 ERROR: firs test message... [in stackoverflow.py:31]
2015-03-08 12:04:04,807 ERROR: firs test message... [in stackoverflow.py:31]
2015-03-08 12:04:13,789 DEBUG: second test message... [in stackoverflow.py:11]
2015-03-08 12:04:14,899 DEBUG: second test message... [in stackoverflow.py:11]

原文由 BennyE_HH 发布,翻译遵循 CC BY-SA 3.0 许可协议

我遇到了同样的问题,以下对我有用:

 app.logger.setLevel(logging.INFO)

原文由 kashaziz 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题