Is there a way to log python print statements in gunicorn?
Question:
With my Procfile like this:
web: gunicorn app:app
--bind "$HOST:$PORT"
--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.
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
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.
-
Specify --log-level
to debug
(default info
).
-
Specify --capture-output
flag (default false).
You should be able to watch logs in error log file.
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.
I use Python3 along with Flask.
I use print('log info')
directly instead of print('log info', flush=True)
.
I only set capture_output
to True
and set a errorlog
file and it works.
Note that:
capture_output
--capture-output
False
Redirect stdout/stderr to specified file in errorlog
I think it’s that you need to specifiy a errorlog file to get the standard output.
Here’s my config file gunicorn.config.py
setting
accesslog = 'gunicorn.log'
errorlog = 'gunicorn.error.log'
capture_output = True
Then run with gunicorn app_py:myapp -c gunicorn.config.py
The equivaluent command line would be
gunicorn app_py:myapp --error-logfile gunicorn.error.log --access-logfile gunicorn.log --capture-output
Personally, I use a trick to print logs. I use file = sys.stderr
as a parameter of the print
statement. Following is the way out:
import sys
print("WHATEVER YOU WANT", file = sys.stderr)
Done!
This is working for me (I’m using a service called si-ct
)
gunicorn --workers 3 --bind unix:si-ct.sock -m 007 wsgi:app --log-level debug --error-logfile - --access-logfile - --capture-output --reload
With that, the print
output shows in my service logs, which I read using
sudo journalctl -u si-ct -f
With my Procfile like this:
web: gunicorn app:app
--bind "$HOST:$PORT"
--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.
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
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.
-
Specify
--log-level
todebug
(defaultinfo
). -
Specify
--capture-output
flag (default false).
You should be able to watch logs in error log file.
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.
I use Python3 along with Flask.
I use print('log info')
directly instead of print('log info', flush=True)
.
I only set capture_output
to True
and set a errorlog
file and it works.
Note that:
capture_output
--capture-output
False
Redirect stdout/stderr to specified file in errorlog
I think it’s that you need to specifiy a errorlog file to get the standard output.
Here’s my config file gunicorn.config.py
setting
accesslog = 'gunicorn.log'
errorlog = 'gunicorn.error.log'
capture_output = True
Then run with gunicorn app_py:myapp -c gunicorn.config.py
The equivaluent command line would be
gunicorn app_py:myapp --error-logfile gunicorn.error.log --access-logfile gunicorn.log --capture-output
Personally, I use a trick to print logs. I use file = sys.stderr
as a parameter of the print
statement. Following is the way out:
import sys
print("WHATEVER YOU WANT", file = sys.stderr)
Done!
This is working for me (I’m using a service called si-ct
)
gunicorn --workers 3 --bind unix:si-ct.sock -m 007 wsgi:app --log-level debug --error-logfile - --access-logfile - --capture-output --reload
With that, the print
output shows in my service logs, which I read using
sudo journalctl -u si-ct -f