Python Flask 不打印到控制台
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44405708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Flask doesn't print to console
提问by X.Z
I'm new to flask, and I'm trying to add print info to debug server side code. When launch my flask app with debug=True, i can't get any info print to console
我是烧瓶新手,我正在尝试添加打印信息来调试服务器端代码。当使用 debug=True 启动我的烧瓶应用程序时,我无法将任何信息打印到控制台
I tried to use logging instead, but no success. So how to debug flask program with console.
我尝试改用日志记录,但没有成功。那么如何用控制台调试flask程序。
@app.route('/getJSONResult', methods=['GET', 'POST'])
def getJSONResult():
if request.method == 'POST':
uut = request.form['uut']
notes = request.form['notes']
temperature = request.form['temperature']
logging.info("enter getJSONReuslt")
print('enter getJSONReuslt')
filter_by_query = {k: v for k, v in {
'uut': uut, 'notes': notes, 'temperature': temperature}.items() if v != ""}
s = session.query(UUT_TEST_INFO).filter_by(**filter_by_query).first()
return jsonify(s.serialize)
if __name__ == '__main__':
app.secret_key = ''.join(random.choice(
string.ascii_uppercase + string.digits) for x in range(32))
app.debug = True
app.run(host='127.0.0.1', port=5000)
> 127.0.0.1 - - [07/Jun/2017 15:20:48] "GET /qyer HTTP/1.1" 200 -
> 127.0.0.1 - - [07/Jun/2017 15:20:48] "GET /static/css/bootstrap.min.css HTTP/1.1" 200 -
> 127.0.0.1 - - [07/Jun/2017 15:20:48] "GET /static/js/bootstrap.min.js HTTP/1.1" 200 -
> 127.0.0.1 - - [07/Jun/2017 15:20:51] "GET /static/css/bootstrap.min.css.map HTTP/1.1" 200 -
> 127.0.0.1 - - [07/Jun/2017 15:21:58] "POST /getJSONResult HTTP/1.1" 500 -
I fixed server side 500 error issue, now request get 200 code, and console displays following info
我修复了服务器端 500 错误问题,现在请求获取 200 代码,并且控制台显示以下信息
$ python project.py
INFO:werkzeug: * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
INFO:werkzeug: * Restarting with stat
WARNING:werkzeug: * Debugger is active!
INFO:werkzeug: * Debugger pin code: 158-624-607
INFO:werkzeug:127.0.0.1 - - [08/Jun/2017 11:33:33] "GET /qyer HTTP/1.1" 200 -
INFO:root:Enter getJSONResult
INFO:werkzeug:127.0.0.1 - - [08/Jun/2017 11:33:43] "POST /getJSONResult HTTP/1.1" 200 -
Still no info from print command
仍然没有来自打印命令的信息
回答by Nurjan
Try this and see if it helps:
试试这个,看看它是否有帮助:
For python2:
对于python2:
from __future__ import print_function
import sys
print('This is error output', file=sys.stderr)
print('This is standard output', file=sys.stdout)
For python3 you don't need to import from futureprint_function:
对于 python3,您不需要从未来的print_function导入:
import sys
print('This is error output', file=sys.stderr)
print('This is standard output', file=sys.stdout)
See if it helps to print to console.
看看它是否有助于打印到控制台。
回答by MrLeeh
By default the level for logging is warning. So you won't see a logging message of level DEBUG
. To fix this just enable debug logging with the basicConfig()
function of the logging module:
默认情况下,日志记录级别为警告。所以你不会看到 level 的日志消息DEBUG
。要解决此问题,只需使用basicConfig()
日志记录模块的功能启用调试日志记录:
import logging
logging.basicConfig(level=logging.DEBUG)
回答by benjy
You can force to flush stdout directly from print:
您可以强制直接从打印中刷新标准输出:
print('enter getJSONReuslt', flush=True)
This way you don't have to print to sys.stderr
(which flushes by default).
这样您就不必打印到sys.stderr
(默认情况下会刷新)。
The reason for your problem is line buffering. Line buffering makes I/O more efficient with the drawback of not immediately showing prints under some conditions.
您的问题的原因是行缓冲。行缓冲使 I/O 更有效率,但缺点是在某些情况下不能立即显示打印件。
回答by GiedriusL
Had the same printing problem. Using sys.stdout.flush()
after the print
solved the issue.
有同样的打印问题。解决问题sys.stdout.flush()
后使用print
。
回答by Bassirou Diaby
you can use the app instance in development mode besause the logging level is set to DEBUG
app.logger.info('This is info output')
in production mode you need to use a more sever level or you can set the logging level to DEBUG
您可以在开发模式下使用应用程序实例,因为app.logger.info('This is info output')
在生产模式下日志记录级别设置为 DEBUG
您需要使用更多服务器级别,或者您可以将日志记录级别设置为 DEBUG
from flask import Flask
import logging
app = Flask(__name__)
logging.basicConfig(level=logging.DEBUG)
@app.route('/')
def hello_world():
app.logger.info('Processing default request')
return 'Hello World!'
if __name__ == '__main__':
app.run()
this article talk about logging into flask https://www.scalyr.com/blog/getting-started-quickly-with-flask-logging/
本文讨论登录烧瓶https://www.scalyr.com/blog/getting-started-quickly-with-flask-logging/