烧瓶JSON请求400日志记录?
问题描述:
我写了我自己的自定义客户端,它通过我的无线网卡发送原始http请求到我的烧瓶网络服务器。烧瓶JSON请求400日志记录?
这是一个典型的请求是什么样子:
Content-Length: 214
User-Agent: blah
Connection: close
Host: 1.2.3.4:5000
Content-Type: application/json
{"data":[{"scoutId":2,"message":"ph=5.65"},{"scoutId":4,"message":"ph=4.28"},{"scoutId":3,"message":"ph=4.28"},{"scoutId":2,"message":"ph=5.65"},{"scoutId":4,"message":"ph=4.28"},{"scoutId":3,"message":"ph=4.30"}]}
有时候,我的客户做砸了,发送畸形的JSON请求到我的烧瓶服务器。通常情况下,烧瓶将只显示:
1.2.3.5 - - [01/Sep/2014 22:13:03] "POST/HTTP/1.1" 400 -
并没有任何关于请求的信息。
我想跟踪在我的环境中产生400的每个请求,并分析导致这些错误的原因。
我在哪里可以将我的自定义错误功能放入烧瓶服务器?
答
尝试开启此:
app.config['TRAP_BAD_REQUEST_ERRORS'] = True
这应该使瓶抛出一个异常,而不是只记录了400(见documentation here)。
如果您需要做的事情还不止这些,使事件处理程序:
http://flask.pocoo.org/docs/0.10/patterns/errorpages/
@app.errorhandler(400)
def page_not_found(exc):
#do something with the exception object `exc` here
....
或者试试/包裹视图函数体中尝试不同的。