skip to Main Content

I am running Flask on a Docker container with wsgi. When I look at the Docker container all I see is the wsgi logs, not the Flask logs. I have added the code below and I don’t see the logs anywhere. For development I will need to see the output that get printed to screen and/or any errors that pop up. Not sure how to capture them.

from flask import Flask
import logging
import logging.handlers

# add for logging, remove for production
handler = logging.handlers.SysLogHandler(address='/var/log')
handler.setFormatter(logging.Formatter('flask [%(levelname)s] %(message)s'))

def create_app():
    app = Flask(__name__)

    #add for logging, remove for production
    app.logger.addHandler(handler)
    
    return app

——– uwsgi.ini ——–

[uwsgi]

module = wsgi:app
master = true
processes = 5


buffer-size = 32768

http = 0.0.0.0:5000

req-logger = file:/var/log/uwsgi/cart-req.log
logger = file:/var/log/uwsgi/cart-err.log

chmod-socket = 660
vacuum = true

die-on-term = true

py-autoreload = 1

2

Answers


  1. Chosen as BEST ANSWER

    This is what worked for me. https://improveandrepeat.com/2021/03/python-friday-63-logging-in-flask/

    from flask import Blueprint, current_app
    import logging
    
    logging.basicConfig(filename='/var/log/flask.log', level=logging.DEBUG, format=f'%(asctime)s %(levelname)s %('
                                                                                      f'name)s %(threadName)s : %(message)s')
    

    and then added this line to print the logs current_app.logger.info("Testing Testing")


  2. Depending on your setup you can use the StreamHandler with stdout:

    handler = logging.StreamHandler(sys.stdout)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search