tornado 修改系统的请求日志

2021-12-21  本文已影响0人  butters001

方法

# 自定义日志消息格式
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方法。我们可以自定义过滤某些系统日志,或者更改日志的样式。
上一篇 下一篇

猜你喜欢

热点阅读