当我将 Flask 应用程序包装在 Gunicorn 中时,写入标准输出似乎不再去任何地方(简单的
print
语句不会出现)。有没有办法将标准输出捕获到gunicorn访问日志中,或者获取访问日志的句柄并直接写入?
使用日志记录:将流设置为标准输出
import logging
import sys
app.logger.addHandler(logging.StreamHandler(sys.stdout))
app.logger.setLevel(logging.DEBUG)
app.logger.debug("Hello World")
这个问题有两种解决方案。它们可能比其他的更长,但最终它们探讨了如何在 Python 中完成日志记录。
关于日志记录的官方 Flask 文档适用于gunicorn。 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 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"
与上面相同的应用程序代码,但没有
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,即 gunicorn -w 2 -b 127.0.0.1:5000 --access-logfile - --log-config logging.ini app:app
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)
我从以下地方得到了这个:https://medium.com/@trstringer/logging-flask-and-gunicorn-the-manageable-way-2e6f0b8beb2f
您可以将标准输出重定向到 errorlog 文件,这对我来说已经足够了。
注意那个:
捕获输出
--capture-output
False
将 stdout/stderr 重定向到 errorlog 中的指定文件
我的配置文件
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