有没有办法在gunicorn中记录python打印语句?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27687867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to log python print statements in gunicorn?
提问by kontur
With my Procfile like this:
我的 Procfile 是这样的:
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.
是否可以通过任何方式将pythonprint
语句记录到stdout/bash?bottle
如果这会影响任何事情,我也在此处使用该框架。
采纳答案by kontur
It turns out the print
statements were actually getting through, but with delay.
事实证明,这些print
陈述实际上正在通过,但有延迟。
The gunicorn docs for --enable-stdio-inheritancenote to set the PYTHONUNBUFFERED
, which I thought I had, but it seems with wrong syntax.
在为--enable-标准输入输出继承gunicorn文档音符设置PYTHONUNBUFFERED
,我认为我有,但它有错误的语法似乎。
I solved it using a .env
file with my foreman
setup to set the variable like this:
我使用一个.env
文件和我的foreman
设置来设置变量来解决它,如下所示:
PYTHONUNBUFFERED=TRUE
回答by Satys
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.
它确实对我有用。
Please specify log-level
to debug
(default info
)http://docs.gunicorn.org/en/stable/settings.html#loglevel,
请指定log-level
到debug
(默认info
)http://docs.gunicorn.org/en/stable/settings.html#loglevel,
Also, specify capture-output
flag (default false)http://docs.gunicorn.org/en/stable/settings.html#capture-output.
此外,指定capture-output
标志(默认为 false)http://docs.gunicorn.org/en/stable/settings.html#capture-output。
You should be able to watch logs in error log file.
您应该能够在错误日志文件中查看日志。
回答by suvtfopw
In python 3, adding flush=True
in each print statement works for my flask/gunicorn app.
在 python 3 中,添加flush=True
每个打印语句适用于我的flask/gunicorn 应用程序。
E.g.
例如
gunicorn --bind 0.0.0.0:8080 server --log-level debug
No particular flags are required.
不需要特别的标志。
See if this helps.
看看这是否有帮助。
回答by Rick
I use Python3 along with Flask.
我将 Python3 与 Flask 一起使用。
I use print('log info')
directly instead of print('log info', flush=True)
.
我print('log info')
直接使用而不是print('log info', flush=True)
.
I only set capture_output
to True
and set a errorlog
file and it works.
我只设置capture_output
到True
并设置errorlog
文件和它的作品。
Note that:
需要注意的是:
capture_output
--capture-output
False
Redirect stdout/stderr to specified file in errorlog
捕获输出
--capture-output
False
将 stdout/stderr 重定向到错误日志中的指定文件
I think it's that you need to specifiy a errorlogfile to get the standard output.
我认为您需要指定一个错误日志文件来获取标准输出。
Here's my config file gunicorn.config.py
setting
这是我的配置文件gunicorn.config.py
设置
accesslog = 'gunicorn.log'
errorlog = 'gunicorn.error.log'
capture_output = True
Then run with gunicorn app_py:myapp -c gunicorn.config.py
然后运行 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
gunicorn app_py:myapp --error-logfile gunicorn.error.log --access-logfile gunicorn.log --capture-output