Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

With my Procfile like this:

web: gunicorn app:app \     --bind "$HOST:$PORT" \     --debug --error-logfile "-" \     --enable-stdio-inheritance \     --reload \     --log-level "debug"  

is it in any way possible to get python print statements to be logged to stdout / bash? I am using the bottle framework here as well, if that affects anything.

like image 980
kontur Avatar asked Dec 29 '14 11:12

kontur


People also ask

How do I get gunicorn logs?

To watch the logs in the console you need to use the option --log-file=- . In version 19.2, Gunicorn logs to the console by default again.

Where are gunicorn logs?

Gunicorn Logs This log file is located at /var/log/cloudify/rest/gunicorn-access. log . The general log mostly has information on Gunicorn workers. It is usually only interesting when there was a problem in starting up the REST service and Flask.


2 Answers

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 
like image 180
kontur Avatar answered Sep 20 '22 05:09

kontur


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.

like image 37
suvtfopw Avatar answered Sep 19 '22 05:09

suvtfopw