有没有办法在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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:08:08  来源:igfitidea点击:

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

pythondebuggingloggingbottlegunicorn

提问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 printstatements to be logged to stdout / bash? I am using the bottleframework here as well, if that affects anything.

是否可以通过任何方式将pythonprint语句记录到stdout/bash?bottle如果这会影响任何事情,我也在此处使用该框架。

采纳答案by kontur

It turns out the printstatements 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 .envfile with my foremansetup 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-levelto debug(default info)http://docs.gunicorn.org/en/stable/settings.html#loglevel,

请指定log-leveldebug(默认infohttp://docs.gunicorn.org/en/stable/settings.html#loglevel

Also, specify capture-outputflag (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=Truein 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_outputto Trueand set a errorlogfile and it works.

我只设置capture_outputTrue并设置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.pysetting

这是我的配置文件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