pythonloggingflask

Change Flask logs from INFO to DEBUG


Flask by default will log things like GET and POST requests directly with an INFO tag. When implementing a custom logger, these are posted to the same logger and clutter my INFO layer. Is there a way to downgrade them to another layer like DEBUG?

This is the logger I use:

# create logger
FORMAT = '%(asctime)s - %(module)s - %(levelname)s - Thread_name: %(threadName)s - %(message)s'
logging.basicConfig(
    format=FORMAT, datefmt='%m/%d/%Y %I:%M:%S %p',
    filename='wizard/logs/example.log', level=logging.DEBUG)

Solution

  • I'm not sure about a way to downgrade log level of requests (as it usually is stated explicitly in the code like logging.info("...")) but the following may help you to reduce the verbosity of Flask itself.

    Python allows you to have multiple loggers, each with its own log level. You can modify any existing logger if you know the module name it is in or the name it was registered with as described here.

    For example:

    import logging
    logger = logging.getLogger("mypackage.mymodule")  # or __name__ for current module
    logger.setLevel(logging.ERROR)
    

    The above can be done for any python module. Flask provides a logger per app. You can get a reference to it like this:

    import logging
    from flask import Flask
    app = Flask(__name__)  # or instead of __name__ provide the name of the module
    app.logger.setLevel(logging.ERROR)