Skip to content

Commit

Permalink
Add logging filter to only print final response
Browse files Browse the repository at this point in the history
  • Loading branch information
Wambere committed Feb 27, 2024
1 parent 576b98c commit 7b17dc2
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions importer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import click
import requests
import logging
import logging.config
import backoff
from datetime import datetime
from oauthlib.oauth2 import LegacyApplicationClient
Expand Down Expand Up @@ -864,6 +865,39 @@ def clean_duplicates(users, cascade_delete):
logging.info("No Practitioners found")


class ResponseFilter(logging.Filter):
def __init__(self, param=None):
self.param = param

def filter(self, record):
if self.param is None:
allow = True
else:
allow = self.param in record.msg
return allow


LOGGING = {
'version': 1,
'filters': {
'custom-filter': {
'()': ResponseFilter,
'param': 'final-response',
}
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'filters': ['custom-filter']
}
},
'root': {
'level': 'INFO',
'handlers': ['console']
},
}


@click.command()
@click.option("--csv_file", required=True)
@click.option("--access_token", required=False)
Expand All @@ -888,6 +922,9 @@ def main(
logging.basicConfig(filename='importer.log', encoding='utf-8', level=logging.ERROR)
logging.getLogger().addHandler(logging.StreamHandler())

if only_response:
logging.config.dictConfig(LOGGING)

start_time = datetime.now()
logging.info("Start time: " + start_time.strftime("%H:%M:%S"))

Expand Down Expand Up @@ -977,8 +1014,7 @@ def main(
else:
logging.error("Empty csv file!")

if only_response:
print(final_response.text)
logging.info("{ \"final-response\": " + final_response.text + "}")

end_time = datetime.now()
logging.info("End time: " + end_time.strftime("%H:%M:%S"))
Expand Down

0 comments on commit 7b17dc2

Please sign in to comment.