pythonflasktornado

logging is not working under Flask in Python 3


I was expecting the console to print the debug statement like "server starting on port...", but when I run python app.py for the following file, nothing prints out. I have already set my app to debug mode.

import logging

from flask import Flask
from flask_env import MetaFlaskEnv
from flask_restful import Resource, Api
from flask_restful import reqparse
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from tornado.wsgi import WSGIContainer


class Configuration(metaclass=MetaFlaskEnv):
    """
    export PORT=80
    """
    DEBUG = True
    PORT = 5000

LOGGER = logging.getLogger(__name__)
LOGGER.setLevel(logging.DEBUG)

app = Flask(__name__)
app.config.from_object(Configuration)
api = Api(app)

@app.route('/')
def hello_world():
    return 'Hello, World!'

if __name__ == '__main__':
    LOGGER.debug("server starting on port :" + str(app.config["PORT"]))
    HTTP_SERVER = HTTPServer(WSGIContainer(app))
    HTTP_SERVER.listen(port=app.config["PORT"])
    IOLoop.instance().start()

Solution

  • try setting log level also on the root logger:

    logging.getLogger().setLevel(logging.DEBUG)
    

    or explicitly redirect the output to stdout using a handler:

    import sys
    import logging
    
    root = logging.getLogger()
    handler = logging.StreamHandler(sys.stdout))
    root.addHandler(handler)