跳过一个端点的 Flask 日志记录?

Vic*_*box 6 python flask

我有一个 Python Flask 应用程序。有一项运行状况检查会多次命中一个端点 (/),我不想在日志中看到它。如何仅对一个 GET 端点禁用日志记录,而将其保留用于其他所有端点?

mpa*_*per 6

艾蒂安·贝尔萨克为我指明了正确的方向。

我是这样实现的:

from werkzeug import serving

parent_log_request = serving.WSGIRequestHandler.log_request


def log_request(self, *args, **kwargs):
    if self.path == '/healthcheck':
        return

    parent_log_request(self, *args, **kwargs)


def filter_healthcheck_logs():
    serving.WSGIRequestHandler.log_request = log_request
Run Code Online (Sandbox Code Playgroud)


Éti*_*sac 4

我建议您实现一个专用的日志过滤器。将过滤器插入内部 werkzeug 记录器。

log_request您还可以在https://github.com/pallets/werkzeug/blob/71cf9902012338f8ee98338fa7bba50572606637/src/werkzeug/serving.py#L378调查子类化 WSGI 请求处理程序方法