0

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
python_user
  • 5,375
  • 2
  • 13
  • 32
NerdGuy021
  • 59
  • 2
  • 6
  • Does this answer your question? [Why Flask logger does not log in docker when using UWSGI in front?](https://stackoverflow.com/questions/51318988/why-flask-logger-does-not-log-in-docker-when-using-uwsgi-in-front) – LSeu Jan 31 '22 at 15:03
  • That is where I got the code above. NO that is not working for me. I already checked that link out before posting this. – NerdGuy021 Jan 31 '22 at 15:05

2 Answers2

0

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

NerdGuy021
  • 59
  • 2
  • 6
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 31 '22 at 16:12
0

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

handler = logging.StreamHandler(sys.stdout)
Simon Zyx
  • 6,503
  • 1
  • 25
  • 37