我是新手,我正在尝试将打印信息添加到调试服务器端代码.使用debug = True启动我的烧瓶应用程序时,我无法将任何信息打印到控制台
我尝试使用日志记录,但没有成功.那么如何使用控制台调试烧瓶程序.
@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 -
Run Code Online (Sandbox Code Playgroud)
我修复了服务器端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 -
Run Code Online (Sandbox Code Playgroud)
仍然没有打印命令的信息
Nur*_*jan 33
试试这个,看看它是否有帮助:
对于python2:
from __future__ import print_function
import sys
print('This is error output', file=sys.stderr)
print('This is standard output', file=sys.stdout)
Run Code Online (Sandbox Code Playgroud)
对于python3,您不需要从将来的 print_function 导入:
import sys
print('This is error output', file=sys.stderr)
print('This is standard output', file=sys.stdout)
Run Code Online (Sandbox Code Playgroud)
看看它是否有助于打印到控制台.
MrL*_*eeh 12
默认情况下,日志记录级别为警告.所以你不会看到级别的日志消息DEBUG.要解决此问题,只需使用basicConfig()日志记录模块的功能启用调试日志记录:
import logging
logging.basicConfig(level=logging.DEBUG)
Run Code Online (Sandbox Code Playgroud)
小智 6
您可以在开发模式下使用应用程序实例,因为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()
Run Code Online (Sandbox Code Playgroud)
本文讨论登录烧瓶https://www.scalyr.com/blog/getting-started-quickly-with-flask-logging/
小智 5
您可以强制从打印直接刷新标准输出:
print('enter getJSONReuslt', flush=True)
Run Code Online (Sandbox Code Playgroud)
这样,您不必打印到sys.stderr(默认情况下会刷新)。
您的问题的原因是行缓冲。行缓冲使I / O更有效,但缺点是在某些情况下不能立即显示打印内容。
| 归档时间: |
|
| 查看次数: |
47842 次 |
| 最近记录: |