I am running a Flask web app using gunicorn+Nginx. I run gunicorn in daemon
mode. I configured gunicorn and nginx to log their access and error to files. But I just cannot get Flask logs to a file.
I use a shell file to start my app with gunicorn:
#!/bin/bash
export VIRTUAL_ENV="/to/virtual/path"
export PATH="$VIRTUAL_ENV/bin:$PATH"
source "$VIRTUAL_ENV/bin/activate"
NAME="hello"
NUM_WORKERS=1
exec gunicorn hello:app \
--name $NAME \
--workers $NUM_WORKERS \
--log-level=debug \
--daemon \
--pid $VIRTUAL_ENV/logs/pid_gunicorn \
--access-logfile $VIRTUAL_ENV/logs/access_gunicorn.log \
--error-logfile $VIRTUAL_ENV/logs/error_gunicorn.log
And in my flask app I add logging as per doc requires:
app.debug = False
...
if __name__ == '__main__':
if app.debug != True:
import logging
from logging.handlers import RotatingFileHandler
handler = RotatingFileHandler("flask.log", maxBytes=10000, backupCount=1)
handler.setLevel(logging.DEBUG)
app.logger.addHandler(handler)
app.logger.debug("test!!")
app.run()
I also added app.logger.debug
at other places.
When I start gunicorn
without --daemon
, the logging file works fine. But once I add --daemon
then no logs are generated.
I tried to use print
but it only works without --daemon
too.
I have searched a while and it seems gunicorn does not support application logging. But I thought logging to a file would be fine?
Does anybody know how I could log out to a file under my settings?