Flask 将标准输出记录到gunicorn访问日志?

Flask 将标准输出记录到gunicorn访问日志?,flask,gunicorn,Flask,Gunicorn,当我用gunicorn包装我的Flask应用程序时,向stdout写入的内容似乎不再适用(简单的print语句不会出现)。是否有办法将标准输出捕获到gunicorn访问日志中,或者获取访问日志的句柄并直接写入它?使用日志记录:将流设置为标准输出 import logging app.logger.addHandler(logging.StreamHandler(sys.stdout)) app.logger.setLevel(logging.DEBUG) app.logger.debug("

当我用gunicorn包装我的Flask应用程序时,向stdout写入的内容似乎不再适用(简单的
print
语句不会出现)。是否有办法将标准输出捕获到gunicorn访问日志中,或者获取访问日志的句柄并直接写入它?

使用日志记录:将流设置为标准输出

import logging

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

app.logger.debug("Hello World")

John mee的解决方案是可行的,但它复制了gunicorn标准输出中的日志条目

我用了这个:

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)

我从:

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

注:

捕获输出
--捕获输出

False

将stdout/stderr重定向到错误日志中的指定文件


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

accesslog = 'gunicorn.log'
errorlog = 'gunicorn.error.log'
capture_output = True
然后使用
gunicorn app\u py:myapp-c gunicorn.config.py运行

等价命令行是


gunicorn应用程序\u py:myapp--错误日志文件gunicorn.error.log--访问日志文件gunicorn.log--捕获输出

此问题的两种解决方案。他们可能比其他人要长,但最终他们会了解Python中如何在后台完成日志记录

1.在Flask应用程序中设置日志记录配置 关于gunicorn伐木工程的官方文件

  • 要尝试的一些示例代码:
  • 使用
    gunicorn运行
  • 使用curl请求它
  • 这将生成以下日志
2.在Gunicorn中设置日志记录配置
  • 与上面相同的应用程序代码,但没有
    dictConfig({…})
    部分

  • 创建一个
    logging.ini
    文件

  • 使用
    --log config logging.ini
    选项运行gunicorn,即
    gunicorn-w 2-b 127.0.0.1:5000--访问日志文件--log config logging.ini应用程序:应用程序

我发现错误不会出现在任何地方,而
print
语句将在没有错误的情况下出现。
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 -w 2 -b 127.0.0.1:5000 --access-logfile - app:app
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"
[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