How to log HTTP response in Tornado?

2019-08-06 06:06发布

I want to be able to log HTTP request and response in tornado. This seems to be easy to do with request:

def log_function(handler):
    info = {
        'Method' : handler.request.method,
        'Host' : handler.request.host,
        'URL' : handler.request.uri
     }

How can the same thing be achieved for response? The response status_code can be retrieved by calling

handler.get_status()

How do I get a body of a response?

1条回答
男人必须洒脱
2楼-- · 2019-08-06 06:21

Tornado doesn't save the response; it sends it directly to the client. If you want to log the response you'll have to save it yourself. You can either do this in your handler code or override the write() and finish() methods to intercept it as it is being written.

查看更多
登录 后发表回答