Flask doesn't print to console

PythonFlask

Python Problem Overview


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

I tried to use logging instead, but no success. So how to debug flask program with console.

@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

$ 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

Python Solutions


Solution 1 - Python

Try this and see if it helps:

For 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 future 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.

Solution 2 - Python

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).

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.

Solution 3 - Python

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:

import logging
logging.basicConfig(level=logging.DEBUG)

Solution 4 - Python

Had the same printing problem. Using sys.stdout.flush() after the print solved the issue.

Solution 5 - Python

You can use the app instance in development mode, because the logging level is set to DEBUG, ie.:

app.logger.info('This is info output')

In production mode you need to use a more severe level or you can set the logging level to DEBUG, ie.:

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 talks about logging into flask: https://www.scalyr.com/blog/getting-started-quickly-with-flask-logging/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionX.ZView Question on Stackoverflow
Solution 1 - PythonNurjanView Answer on Stackoverflow
Solution 2 - PythonbenjyView Answer on Stackoverflow
Solution 3 - PythonMrLeehView Answer on Stackoverflow
Solution 4 - PythonGiedriusLView Answer on Stackoverflow
Solution 5 - PythonBassirou DiabyView Answer on Stackoverflow