OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Simple log filtering with Flask

  • Thread starter Thread starter Eric Gottesman
  • Start date Start date
E

Eric Gottesman

Guest
I'm new to Flask and I'm having trouble filtering out some logs. My use case is simple: I don't want to log health check queries, which hit the route /health.

Here's what I have:

Code:
from flask import Flask
from flask.logging import logging

class NoHealth(logging.Filter):
    def filter(self, record):
        return 'GET /health' not in record.getMessage()

no_health = NoHealth()
app = Flask(__name__)
app.logger.addFilter(no_health)
app.logger.setLevel('INFO')

@app.route('/health')
def health_check():
    return "OK"

The logs I want to drop look like this:

127.0.0.1 - - [31/Mar/2020 17:51:03] "GET /health HTTP/1.1" 200 -

They're still coming through, however. What am I missing?
<p>I'm new to Flask and I'm having trouble filtering out some logs. My use case is simple: I don't want to log health check queries, which hit the route <code>/health</code>.</p>

<p>Here's what I have:</p>

<pre><code>from flask import Flask
from flask.logging import logging

class NoHealth(logging.Filter):
def filter(self, record):
return 'GET /health' not in record.getMessage()

no_health = NoHealth()
app = Flask(__name__)
app.logger.addFilter(no_health)
app.logger.setLevel('INFO')

@app.route('/health')
def health_check():
return "OK"
</code></pre>

<p>The logs I want to drop look like this:</p>

<p><code>127.0.0.1 - - [31/Mar/2020 17:51:03] "GET /health HTTP/1.1" 200 -</code></p>

<p>They're still coming through, however. What am I missing?</p>
 

Latest posts

Online statistics

Members online
0
Guests online
4
Total visitors
4
Top