gpt4 book ai didi

python - flask 记录根本不起作用

转载 作者:IT老高 更新时间:2023-10-28 21:10:01 24 4
gpt4 key购买 nike

我正在尝试将 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('正在测试...')

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

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

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 -

最佳答案

您的(调试)日志消息被 Flask 抑制,因为您没有在 Debug模式下运行。如果您将以下标志设置为 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]

关于python - flask 记录根本不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28925451/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com