将stdout记录到gunicorn访问日志?

时间:2014-01-28 07:47:00

标签: flask gunicorn

当我将我的Flask应用程序用gunicorn写入stdout时,似乎不再去任何地方(简单的print语句不会出现)。有没有办法将stdout捕获到gunicorn访问日志中,或者获取访问日志的句柄并直接写入它?

4 个答案:

答案 0 :(得分:11)

使用日志记录:将流设置为stdout

import logging

app.logger.addHandler(logging.StreamHandler(sys.stdout))
app.logger.setLevel(logging.DEBUG)

app.logger.debug("Hello World")

答案 1 :(得分:0)

John mee的解决方案有效,但它会复制gunicorn的stdout中的日志条目。

我用了这个:

import logging
from flask import Flask

app = Flask(__name__)

if __name__ != '__main__':
    gunicorn_logger = logging.getLogger('gunicorn.error')
    app.logger.handlers = gunicorn_logger.handlers
    app.logger.setLevel(gunicorn_logger.level)

并从https://medium.com/@trstringer/logging-flask-and-gunicorn-the-manageable-way-2e6f0b8beb2f

处获得了此信息。

答案 2 :(得分:0)

您可以将标准输出重定向到错误日志文件,对我来说足够了。

注意that

  

捕获输出

     

--capture-output
  False
   将stdout / stderr重定向到错误日志

中的指定文件

我的配置文件gunicorn.config.py设置

accesslog = 'gunicorn.log'
errorlog = 'gunicorn.error.log'
capture_output = True

然后使用gunicorn app_py:myapp -c gunicorn.config.py

运行

等值命令行为

gunicorn app_py:myapp --error-logfile gunicorn.error.log --access-logfile gunicorn.log --capture-output

答案 3 :(得分:0)

此问题的两种解决方案。它们可能比其他的更长,但是最终他们利用Python进行了后台日志记录。

1。在Flask应用中设置日志记录配置

Flask官方文档中有关枪支的记录。 https://flask.palletsprojects.com/en/1.1.x/logging/#basic-configuration

  • 一些示例代码可以尝试:
from logging.config import dictConfig

from flask import Flask

dictConfig(
    {
        "version": 1,
        "disable_existing_loggers": False,
        "formatters": {
            "default": {
                "format": "[%(asctime)s] [%(process)d] [%(levelname)s] in %(module)s: %(message)s",
                "datefmt": "%Y-%m-%d %H:%M:%S %z"
            }
        },
        "handlers": {
            "wsgi": {
                "class": "logging.StreamHandler",
                "stream": "ext://flask.logging.wsgi_errors_stream",
                "formatter": "default",
            }
        },
        "root": {"level": "DEBUG", "handlers": ["wsgi"]},
    }
)
app = Flask(__name__)

@app.route("/")
def hello():
    app.logger.debug("this is a DEBUG message")
    app.logger.info("this is an INFO message")
    app.logger.warning("this is a WARNING message")
    app.logger.error("this is an ERROR message")
    app.logger.critical("this is a CRITICAL message")
    return "hello world"
  • 运行gunicorn
gunicorn -w 2 -b 127.0.0.1:5000 --access-logfile - app:app
  • 使用curl请求它
curl http://127.0.0.1:5000
  • 这将生成以下日志
[2020-09-04 11:24:43 +0200] [2724300] [INFO] Starting gunicorn 20.0.4
[2020-09-04 11:24:43 +0200] [2724300] [INFO] Listening at: http://127.0.0.1:5000 (2724300)
[2020-09-04 11:24:43 +0200] [2724300] [INFO] Using worker: sync
[2020-09-04 11:24:43 +0200] [2724311] [INFO] Booting worker with pid: 2724311
[2020-09-04 11:24:43 +0200] [2724322] [INFO] Booting worker with pid: 2724322
[2020-09-04 11:24:45 +0200] [2724322] [DEBUG] in flog: this is a DEBUG message
[2020-09-04 11:24:45 +0200] [2724322] [INFO] in flog: this is an INFO message
[2020-09-04 11:24:45 +0200] [2724322] [WARNING] in flog: this is a WARNING message
[2020-09-04 11:24:45 +0200] [2724322] [ERROR] in flog: this is an ERROR message
[2020-09-04 11:24:45 +0200] [2724322] [CRITICAL] in flog: this is a CRITICAL message
127.0.0.1 - - [04/Sep/2020:11:24:45 +0200] "GET / HTTP/1.1" 200 11 "-" "curl/7.68.0"

2。在Gunicorn中设置日志记录配置

  • 与上面相同的应用程序代码,但没有dictConfig({...})部分

  • 创建一个logging.ini文件

[loggers]
keys=root

[handlers]
keys=consoleHandler

[formatters]
keys=simpleFormatter

[logger_root]
level=DEBUG
handlers=consoleHandler

[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)

[formatter_simpleFormatter]
format=[%(asctime)s] [%(process)d] [%(levelname)s] - %(module)s - %(message)s
datefmt=%Y-%m-%d %H:%M:%S %z
  • 使用--log-config logging.ini选项(即gunicorn -w 2 -b 127.0.0.1:5000 --access-logfile - --log-config logging.ini app:app