Is there a way to log python print statements in gunicorn?

It turns out the print statements were actually getting through, but with delay.

The gunicorn docs for --enable-stdio-inheritance note to set the PYTHONUNBUFFERED, which I thought I had, but it seems with wrong syntax.

I solved it using a .env file with my foreman setup to set the variable like this:

PYTHONUNBUFFERED=TRUE

In python 3, adding flush=True in each print statement works for my flask/gunicorn app.

E.g.

gunicorn --bind 0.0.0.0:8080 server --log-level debug

No particular flags are required.

See if this helps.


Please try below command:

gunicorn --workers 3 --bind 127.0.0.1:5000 --error-logfile /var/log/gunicorn/error.log --access-logfile /var/log/gunicorn/access.log --capture-output --log-level debug

It did work for me.

  1. Specify --log-level to debug(default info).

  2. Specify --capture-output flag (default false).

You should be able to watch logs in error log file.