Connexion Flask 日志格式
Connexion Flask logging formatting
我正在努力使用 gevent 服务器为 connexion flask rest api 设置日志记录。我想只记录每个请求后的消息,如 GET /health HTTP/1.1
或更好的只是 http 状态代码,例如200
。我按照 flask docu 并在创建应用程序之前设置了 dictConfig
。像这样:
import connexion
from logging.config import dictConfig
def main():
app = connexion.App(__name__)
app.app.logger.info('This is a check!')
app.add_api('swagger.yaml')
app.run(server='gevent')
if __name__ == '__main__':
dictConfig({
'version': 1,
'formatters': {'default': {
'format': 'Custom message: %(message)s',
}},
'handlers': {'wsgi': {
'class': 'logging.StreamHandler',
'stream': 'ext://flask.logging.wsgi_errors_stream',
'formatter': 'default'
}},
'root': {
'level': 'INFO',
'handlers': ['wsgi']
}
})
main()
启动应用并发送两个请求(一个 200 和一个 405)后:
Custom message: This is a check!
127.0.0.1 - - [2021-03-15 12:42:35] "GET /health HTTP/1.1" 200 126 0.001714
127.0.0.1 - - [2021-03-15 12:42:35] "PUT /health HTTP/1.1" 405 275 0.000711
因此,在 app.run()
之前,app.app.logging()
看起来不错,只发送消息 Custom message: This is a check!
,但之后日志不再遵循字典。
知道我做错了什么吗?
据我所知,dictConfig 没有被使用,因为你没有将它分配给任何东西。
你需要像
这样的东西
logging.config.dictConfig(yourconfig)
其中 yourconfig 是您的 dictConfig。将您的 dictConfig 移动到 main() 以使其处于同一范围内,或者如果您选择将其分开,则从文件中导入它。
我正在努力使用 gevent 服务器为 connexion flask rest api 设置日志记录。我想只记录每个请求后的消息,如 GET /health HTTP/1.1
或更好的只是 http 状态代码,例如200
。我按照 flask docu 并在创建应用程序之前设置了 dictConfig
。像这样:
import connexion
from logging.config import dictConfig
def main():
app = connexion.App(__name__)
app.app.logger.info('This is a check!')
app.add_api('swagger.yaml')
app.run(server='gevent')
if __name__ == '__main__':
dictConfig({
'version': 1,
'formatters': {'default': {
'format': 'Custom message: %(message)s',
}},
'handlers': {'wsgi': {
'class': 'logging.StreamHandler',
'stream': 'ext://flask.logging.wsgi_errors_stream',
'formatter': 'default'
}},
'root': {
'level': 'INFO',
'handlers': ['wsgi']
}
})
main()
启动应用并发送两个请求(一个 200 和一个 405)后:
Custom message: This is a check!
127.0.0.1 - - [2021-03-15 12:42:35] "GET /health HTTP/1.1" 200 126 0.001714
127.0.0.1 - - [2021-03-15 12:42:35] "PUT /health HTTP/1.1" 405 275 0.000711
因此,在 app.run()
之前,app.app.logging()
看起来不错,只发送消息 Custom message: This is a check!
,但之后日志不再遵循字典。
知道我做错了什么吗?
据我所知,dictConfig 没有被使用,因为你没有将它分配给任何东西。
你需要像
这样的东西logging.config.dictConfig(yourconfig)
其中 yourconfig 是您的 dictConfig。将您的 dictConfig 移动到 main() 以使其处于同一范围内,或者如果您选择将其分开,则从文件中导入它。