方法
# 自定義日志消息格式
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方法赶掖。我們可以自定義過濾某些系統(tǒng)日志,或者更改日志的樣式矩父。