如何记录swagger / connexion请求体?

问题描述 投票:0回答:1

我正在使用connexion和Swagger在Python中创建API。我想将所有传入的呼叫记录到一个文件中,以便我可以看到所请求的内容。我已经能够记录我收到的电话的路径,但我无法弄清楚如何记录身体。

这是我配置日志的地方:

if __name__ == '__main__':
    import logging
    logging.basicConfig(format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s',                            filename='golden_record.log',level=logging.DEBUG)

当我查看日志时,我会看到路径,如下面的POST呼叫...

2019-03-23 12:47:16,182 - 工具 - 信息 - 127.0.0.1 - - [23 / Mar / 2019 12:47:16]“POST / golden_record / account HTTP / 1.1”400 -

但我没有看到通话的主体(即我发送的数据)。有没有办法自动记录每个进来的电话?这样做有助于调试无法正常运行的调用。谢谢!

python-logging connexion
1个回答
0
投票

只需添加一个Flask before_request。 Connexion实例将Flask实例存储为app属性,因此您可以使用app.app进行访问。

添加代码,您将记录正文:

@app.app.before_request def log_request_info(): print('Body: %s', request.get_data())

print方法更改为记录器:

@app.app.before_request def log_request_info(): logger.info('Body: %s', request.get_data())

request导入是:

from flask import request

app是你的Connexion实例:

app = connexion.App(__name__, specification_dir="./swagger/")

我用代码创建了一个Gist。 https://gist.github.com/kevinmmartins/f832c21215bb51cea73b8fdd28f6d88d

© www.soinside.com 2019 - 2024. All rights reserved.