[英]How to make Flask log to stdout instead of stderr?
Flask does all kinds of logging automatically, for example when receiving a POST request Flask will automatically log it: Flask 会自动进行各种日志记录,例如当收到 POST 请求时 Flask 会自动记录它:
127.0.0.1 - - [05/Jul/2019 18:18:16] "POST /test/ HTTP/1.1" 200 -
The problem is that this logging is done to stderr
, I would like it to instead do all the same logging, with the default formatting, but log to sys.stdout
instead.问题是此日志记录是针对stderr
完成的,我希望它使用默认格式执行所有相同的日志记录,但改为记录到sys.stdout
。
I've tried something like this:我试过这样的事情:
import logging
import sys
app = flask.Flask(__name__)
handler = logging.StreamHandler(sys.stdout)
app.logger.addHandler(handler)
And based on flask documentation I tried:根据我尝试过的烧瓶文档:
import sys
from logging.config import dictConfig
dictConfig({
'version': 1,
'formatters': {'default': {
'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s',
}},
'handlers': {'wsgi': {
'class': 'logging.StreamHandler',
'stream': 'sys.stdout',
'formatter': 'default'
}},
'root': {
'level': 'INFO',
'handlers': ['wsgi']
}
})
app = flask.Flask(__name__)
But the first one doesn't have the desired effect and the second one just crashes.但是第一个没有达到预期的效果,第二个只是崩溃。
Based on @RomanPerekhrest 's comment, this did the job:根据@RomanPerekhrest的评论,这完成了工作:
import sys
from logging.config import dictConfig
dictConfig({
'version': 1,
'formatters': {'default': {
'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s',
}},
'handlers': {'wsgi': {
'class': 'logging.StreamHandler',
'stream': 'ext://sys.stdout',
'formatter': 'default'
}},
'root': {
'level': 'INFO',
'handlers': ['wsgi']
}
})
app = flask.Flask(__name__)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.