Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip Flask logging for one endpoint?

Tags:

python

flask

I have a Python Flask application. There is a healthcheck that hits one endpoint (/) a lot, and I'd like to not see it in logs. How do I disable logging for only one GET endpoint, and leave it be for everything else?

like image 541
Victor Pudeyev Avatar asked Jul 09 '19 19:07

Victor Pudeyev


2 Answers

Étienne Bersac pointed me in the right direction.

This is how I implemented it:

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
like image 56
mpaepper Avatar answered Sep 30 '22 19:09

mpaepper


I suggest you implement a dedicated logging filter. Plug that filter on the internal werkzeug logger.

You can also investigate subclassing WSGI request handler log_request method at https://github.com/pallets/werkzeug/blob/71cf9902012338f8ee98338fa7bba50572606637/src/werkzeug/serving.py#L378

like image 36
Étienne Bersac Avatar answered Sep 30 '22 20:09

Étienne Bersac