Gunicorn: 如何将 accesslog 打印到 STDOUT?

创建于 2016-01-19  ·  22评论  ·  资料来源: benoitc/gunicorn

你好,

http://docs.gunicorn.org/en/stable/settings.html#accesslog告诉我'-'将输出到 stderr 但我们更喜欢 STDOUT 用于记录目的。 有没有办法做到这一点?

谢谢!

Documentation FeaturLogging

最有用的评论

使用应用程序文件myapp.py ,我尝试了以下操作。

$ python --version
Python 3.5.2
$ gunicorn --version
gunicorn (version 19.7.0)
$ gunicorn --access-logfile - myapp:app 

我可以确认访问日志输出到stdout

所有22条评论

相关行在这里: https://github.com/benoitc/gunicorn/blame/master/gunicorn/glogging.py#L335 logging.StreamHandler()默认为sys.stderr 。 也许我们可以更改 accesslog 以接受“stdout”和“stderr”作为有效值并传递给logging.StreamHandler()

另一个但不太灵活的解决方案:

控制台处理程序已经打印到sys.stdouthttps://github.com/benoitc/gunicorn/blob/master/gunicorn/glogging.py#L64我们可以将处理程序设置为gunicorn.accesslog的“控制台” https://github.com/benoitc/gunicorn/blob/master/gunicorn/glogging.py#L55中为gunicorn.error所做的那样

我糊涂了。 StreamHandler 不是已经登录 sys.stdout 了吗?

不,默认值为sys.stderrhttps ://github.com/python/cpython/blob/master/Lib/logging/__init__.py#L955

好的我现在明白了。 最初我们将日志打印到标准输出。 IMO 访问日志应默认为 STDOUT。 想法?

@benoitc :+1:

在标准输出上访问日志
stderr 上的错误日志

有关此请求的任何更新?

@benoitc我不确定这个提交是否解决了这个问题。 假设我在没有额外参数的情况下运行gunicorn myapp:app 。 期望 gunicorn 应该将访问记录记录到标准输出吗? 如果是这样,gunicorn 不会那样做。

https://github.com/benoitc/gunicorn/blob/master/gunicorn/glogging.py#L302没有任何命令行参数self.cfg.accesslogNone并且函数提前返回。 据我所知,如果不指定--log-config--log-syslog ,似乎没有办法登录到标准输出。 这个对吗?

如果这是您的意思,默认情况下访问日志被禁用?

@benoitc是的。 上面的讨论似乎表明访问日志将转到标准输出,而错误日志将转到标准错误。 但是,默认情况下似乎禁用了访问日志记录。

顺便说一句,我曾经使用 Python 2.7 能够做到--access-log /dev/stdout --error-log /dev/stderr并且这会起作用。 使用 Python 3,在https://github.com/benoitc/gunicorn/blob/f722a6eb65e51407c420ac3d6be72d03bd773ad0/gunicorn/util.py#L498中将/dev/stdout作为参数传递失败

👍 @suriya我想我正在寻找与您相同的行为 - 我想运行--capture-log并启用--access-logfile=<something> ,但我希望我的访问日志行转到标准输出不是标准错误。

编辑:澄清一下,这似乎只是 Python3 的一个问题。 使用 python3.5.1,将--access-logfile=/dev/stdout传递给 gunicorn 无法启动服务器。

是的,默认情况下似乎禁用了访问日志记录。

@huoxy除了默认值之外,我认为问题更多在于没有简单的方法将访问日志写入标准输出。

@huoxy ˋ--access-logfile=-` 应该可以解决问题。 如果没有,让我知道。

  • 贝努瓦
    2016 年 7 月 27 日星期三 08:49,Suriya Subramanian [email protected]
    写道:

@huoxy https://github.com/huoxy比默认值更多,我想
问题更多的是没有简单的方法可以将访问日志写入
标准输出。


你收到这个是因为你被提到了。
直接回复此邮件,在 GitHub 上查看
https://github.com/benoitc/gunicorn/issues/1184#issuecomment -235501347,
或使线程静音
https://github.com/notifications/unsubscribe-auth/AAA4oj67GbLqwaJaCQ9NOH7-iAXJoMxGks5qZv9fgaJpZM4HHW4G
.

这也可能是一个文档问题。 但必须将访问日志打印到标准输出
当使用-时。 否则这是一个错误。 让我知道...
2016 年 7 月 27 日星期三 09:03,Benoit Chesneau [email protected]写道:

@huoxy ˋ--access-logfile=-` 应该可以解决问题。 如果没有,让我知道。

  • 贝努瓦
    2016 年 7 月 27 日星期三 08:49,Suriya Subramanian [email protected]
    写道:

@huoxy https://github.com/huoxy比默认值更多,我想
问题更多的是没有简单的方法可以将访问日志写入
标准输出。


你收到这个是因为你被提到了。
直接回复此邮件,在 GitHub 上查看
https://github.com/benoitc/gunicorn/issues/1184#issuecomment -235501347,
或使线程静音
https://github.com/notifications/unsubscribe-auth/AAA4oj67GbLqwaJaCQ9NOH7-iAXJoMxGks5qZv9fgaJpZM4HHW4G
.

@benoitc如 #1293 中所述, gunicorn --access-log -记录对标准错误的访问。 我尝试使用gunicorn --access-log=-并且日志仍然出现标准错误。

根据这个:
https://github.com/gunicorn/gunicorn/blob/master/gunicorn/glogging.py#L58

默认记录器是console ,它输出到标准输出,所以我想如果你像我一样需要标准输出的所有内容,为了让所有内容都到达 gunicorn 日志记录驱动程序,你可以将这些选项留空。

另外,我建议的解决方案是为日志文件创建一个符号链接到/dev/stdout/dev/stderror如果它不能直接工作:

ln -sf /var/log/gunicorn.access.log  /dev/stdout
ln -sf /var/log/gunicorn.error.log  /dev/stdout

exec newrelic-admin run-program gunicorn --chdir /usr/src/app --name nexchange --bind 0.0.0.0:${GUNICORN_PORT} --workers 3 --log-level=info --log-file=/var/log/gunicorn.error.log --access-logfile=/var/log/gunicorn.access.log nexchange.wsgi:application "$@"

我刚搬到上周发布的 gunicorn 19.7.0。 不幸的是,我仍然无法将访问日志写入除 stderr 之外的任何内容。

我已经尝试在没有选项的情况下运行 gunicorn,并且还尝试了包含/不包含“--access-logfile=---error-logfile=---log-level=DEBUG”的所有排列。

我是否缺少有关 gunicorn 日志记录的一些基本知识? 我的环境或 Django 中的某些东西可能会溢出并导致此问题吗?

使用应用程序文件myapp.py ,我尝试了以下操作。

$ python --version
Python 3.5.2
$ gunicorn --version
gunicorn (version 19.7.0)
$ gunicorn --access-logfile - myapp:app 

我可以确认访问日志输出到stdout

我在 Python 2.7.14 上。 (不知何故错过了几个月前的快速回复。)

您也可以在 gunicorn.config.py 中使用accesslog = "-"

此页面是否有帮助?
0 / 5 - 0 等级