Skip to content

Commit

Permalink
Add a configurable filter for logs
Browse files Browse the repository at this point in the history
  • Loading branch information
kikkomep committed Mar 24, 2022
1 parent 71a7686 commit 97f8518
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
36 changes: 35 additions & 1 deletion lifemonitor/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import configparser
import logging
import os
from logging.config import dictConfig
Expand Down Expand Up @@ -163,6 +164,32 @@ def get_config_by_name(name, settings=None):
return ProductionConfig


class LogFilter(logging.Filter):
def __init__(self, param=None):
self.param = param
try:
config = configparser.ConfigParser()
config.read('setup.cfg')
self.filters = [_.strip() for _ in config.get('logging', 'filters').split(',')]
except Exception:
self.filters = []

def filter(self, record):
try:
filtered = False
for k in self.filters:
if k in record.name:
filtered = True
if not filtered:
if 'Requester' in record.name:
record.msg = "Request to Github API"
logger.debug("Request args: %r %r %r %r", record.args[0], record.args[1], record.args[2], record.args[3])
record.args = None
except Exception as e:
logger.exception(e)
return not filtered


def configure_logging(app):
level_str = app.config.get('LOG_LEVEL', 'INFO')
error = False
Expand All @@ -177,10 +204,17 @@ def configure_logging(app):
'formatters': {'default': {
'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s',
}},
'filters': {
'myfilter': {
'()': LogFilter,
# 'param': '',
}
},
'handlers': {'wsgi': {
'class': 'logging.StreamHandler',
'stream': 'ext://flask.logging.wsgi_errors_stream',
'formatter': 'default'
'formatter': 'default',
'filters': ['myfilter']
}},
'response': {
'level': logging.INFO,
Expand Down
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ VCS = git
style = pep440
versionfile_source = lifemonitor/_version.py
tag_prefix =

[logging]
filters = connexion,validator

0 comments on commit 97f8518

Please sign in to comment.