Flask 日志记录根本不起作用
Flask logging not working at all
我正在尝试将 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()
有几个问题:
- 没有
output.log
文件生成
只有第一条日志消息有效:
app.logger.error('testing...')
并且仅在标准输出中...视图“/”中的那个甚至不打印到标准输出...我做错了什么吗?
这是启动应用程序并转到 /:
的输出
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 抑制,因为您未 运行 处于调试模式。如果您将以下标志设置为 True,您的代码将起作用。
app.run(debug=True)
消息现在将按预期显示。
BennyE$ python3 Whosebug.py
2015-03-08 12:04:04,650 ERROR: firs test message... [in Whosebug.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 Whosebug.py:31]
--------------------------------------------------------------------------------
DEBUG in Whosebug [Whosebug.py:11]:
second test message...
--------------------------------------------------------------------------------
2015-03-08 12:04:13,789 DEBUG: second test message... [in Whosebug.py:11]
192.168.178.23 - - [08/Mar/2015 12:04:13] "GET / HTTP/1.1" 200 -
--------------------------------------------------------------------------------
DEBUG in Whosebug [Whosebug.py:11]:
second test message...
--------------------------------------------------------------------------------
2015-03-08 12:04:14,899 DEBUG: second test message... [in Whosebug.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 Whosebug.py:31]
2015-03-08 12:04:04,650 ERROR: firs test message... [in Whosebug.py:31]
2015-03-08 12:04:04,807 ERROR: firs test message... [in Whosebug.py:31]
2015-03-08 12:04:13,789 DEBUG: second test message... [in Whosebug.py:11]
2015-03-08 12:04:14,899 DEBUG: second test message... [in Whosebug.py:11]
谢谢 BennyE_HH,它有效。
但是 Flask 没有抑制 ERROR 级别的日志消息,即使调试模式已禁用(默认为禁用)。
我认为我们应该调用 app.logger.setLevel(logging.DEBUG)
来控制日志级别,即使调试模式为 false。
我遇到了同样的问题,以下对我有用:
app.logger.setLevel(logging.INFO)
按照描述在根记录器上设置它
logging.getLogger().setLevel(logging.DEBUG)
我正在尝试将 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()
有几个问题:
- 没有
output.log
文件生成 只有第一条日志消息有效:
app.logger.error('testing...')
并且仅在标准输出中...视图“/”中的那个甚至不打印到标准输出...我做错了什么吗?
这是启动应用程序并转到 /:
的输出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 抑制,因为您未 运行 处于调试模式。如果您将以下标志设置为 True,您的代码将起作用。
app.run(debug=True)
消息现在将按预期显示。
BennyE$ python3 Whosebug.py
2015-03-08 12:04:04,650 ERROR: firs test message... [in Whosebug.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 Whosebug.py:31]
--------------------------------------------------------------------------------
DEBUG in Whosebug [Whosebug.py:11]:
second test message...
--------------------------------------------------------------------------------
2015-03-08 12:04:13,789 DEBUG: second test message... [in Whosebug.py:11]
192.168.178.23 - - [08/Mar/2015 12:04:13] "GET / HTTP/1.1" 200 -
--------------------------------------------------------------------------------
DEBUG in Whosebug [Whosebug.py:11]:
second test message...
--------------------------------------------------------------------------------
2015-03-08 12:04:14,899 DEBUG: second test message... [in Whosebug.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 Whosebug.py:31]
2015-03-08 12:04:04,650 ERROR: firs test message... [in Whosebug.py:31]
2015-03-08 12:04:04,807 ERROR: firs test message... [in Whosebug.py:31]
2015-03-08 12:04:13,789 DEBUG: second test message... [in Whosebug.py:11]
2015-03-08 12:04:14,899 DEBUG: second test message... [in Whosebug.py:11]
谢谢 BennyE_HH,它有效。
但是 Flask 没有抑制 ERROR 级别的日志消息,即使调试模式已禁用(默认为禁用)。
我认为我们应该调用 app.logger.setLevel(logging.DEBUG)
来控制日志级别,即使调试模式为 false。
我遇到了同样的问题,以下对我有用:
app.logger.setLevel(logging.INFO)
按照描述在根记录器上设置它
logging.getLogger().setLevel(logging.DEBUG)