美文网首页
tornado 修改系统的请求日志

tornado 修改系统的请求日志

作者: butters001 | 来源:发表于2021-12-21 16:49 被阅读0次

方法

# 自定义日志消息格式
def log_request(handler):
    if handler.get_status() < 400:
        log_method = access_log.info
    elif handler.get_status() < 500:
        log_method = access_log.warning
    else:
        log_method = access_log.error
    request_time = 1000.0 * handler.request.request_time()
    # 自定义过滤 
    if handler.get_status() <= 400 and handler.request.path.startswith("/abc"):
        pass
    else:
        request_time = 1000.0 * handler.request.request_time()
        log_method(
            "%d %s %.2fms",
            handler.get_status(),
            handler._request_summary(),
            request_time,
        )

app.settings["log_function"] = log_request

原理

由 tornado 源码可知:

image.png
我们在上面对log_function进行了赋值,所以就不会走默认的log方法了,而是走我们自定义的log方法。我们可以自定义过滤某些系统日志,或者更改日志的样式。

相关文章

网友评论

      本文标题:tornado 修改系统的请求日志

      本文链接:https://www.haomeiwen.com/subject/vemxqrtx.html