diff --git a/README.md b/README.md index 4e75af9..7a8ca70 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # DRF API Logger -![version](https://img.shields.io/badge/version-1.1.15-blue.svg) +![version](https://img.shields.io/badge/version-1.1.16-blue.svg) [![Downloads](https://static.pepy.tech/personalized-badge/drf-api-logger?period=total&units=none&left_color=black&right_color=orange&left_text=Downloads%20Total)](http://pepy.tech/project/drf-api-logger) [![Downloads](https://static.pepy.tech/personalized-badge/drf-api-logger?period=month&units=none&left_color=black&right_color=orange&left_text=Downloads%20Last%20Month)](https://pepy.tech/project/drf-api-logger) [![Open Source](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)](https://opensource.org/) diff --git a/drf_api_logger/admin.py b/drf_api_logger/admin.py index 7085acd..159a00f 100644 --- a/drf_api_logger/admin.py +++ b/drf_api_logger/admin.py @@ -24,7 +24,7 @@ def export_as_csv(self, request, queryset): writer.writerow(field_names) for obj in queryset: - row = writer.writerow([getattr(obj, field) for field in field_names]) + writer.writerow([getattr(obj, field) for field in field_names]) return response @@ -39,7 +39,7 @@ class SlowAPIsFilter(admin.SimpleListFilter): def __init__(self, request, params, model, model_admin): super().__init__(request, params, model, model_admin) if hasattr(settings, 'DRF_API_LOGGER_SLOW_API_ABOVE'): - if type(settings.DRF_API_LOGGER_SLOW_API_ABOVE) == int: # Making sure for integer value. + if isinstance(settings.DRF_API_LOGGER_SLOW_API_ABOVE, int): # Making sure for integer value. self._DRF_API_LOGGER_SLOW_API_ABOVE = settings.DRF_API_LOGGER_SLOW_API_ABOVE / 1000 # Converting to seconds. def lookups(self, request, model_admin): @@ -83,10 +83,10 @@ def __init__(self, model, admin_site): super().__init__(model, admin_site) self._DRF_API_LOGGER_TIMEDELTA = 0 if hasattr(settings, 'DRF_API_LOGGER_SLOW_API_ABOVE'): - if type(settings.DRF_API_LOGGER_SLOW_API_ABOVE) == int: # Making sure for integer value. + if isinstance(settings.DRF_API_LOGGER_SLOW_API_ABOVE, int): # Making sure for integer value. self.list_filter += (SlowAPIsFilter,) if hasattr(settings, 'DRF_API_LOGGER_TIMEDELTA'): - if type(settings.DRF_API_LOGGER_TIMEDELTA) == int: # Making sure for integer value. + if isinstance(settings.DRF_API_LOGGER_TIMEDELTA, int): # Making sure for integer value. self._DRF_API_LOGGER_TIMEDELTA = settings.DRF_API_LOGGER_TIMEDELTA def added_on_time(self, obj): @@ -113,7 +113,7 @@ def changelist_view(self, request, extra_context=None): response = super(APILogsAdmin, self).changelist_view(request, extra_context) try: filtered_query_set = response.context_data["cl"].queryset - except: + except Exception: return response analytics_model = filtered_query_set.values('added_on__date').annotate(total=Count('id')).order_by('total') status_code_count_mode = filtered_query_set.values('id').values('status_code').annotate( diff --git a/drf_api_logger/events.py b/drf_api_logger/events.py index a475e9c..99db1e7 100644 --- a/drf_api_logger/events.py +++ b/drf_api_logger/events.py @@ -23,7 +23,7 @@ def __init__(self, events=None): try: iter(events) - except: + except Exception: raise AttributeError("type object %s is not iterable" % (type(events))) else: diff --git a/drf_api_logger/middleware/api_logger_middleware.py b/drf_api_logger/middleware/api_logger_middleware.py index 8134608..bc1f244 100644 --- a/drf_api_logger/middleware/api_logger_middleware.py +++ b/drf_api_logger/middleware/api_logger_middleware.py @@ -13,11 +13,6 @@ from drf_api_logger.start_logger_when_server_starts import LOGGER_THREAD from drf_api_logger.utils import get_headers, get_client_ip, mask_sensitive_data -""" -File: api_logger_middleware.py -Class: APILoggerMiddleware -""" - class APILoggerMiddleware: def __init__(self, get_response): @@ -121,7 +116,7 @@ def __call__(self, request): Ignore the request body if larger then specified. """ request_data = '' - except: + except Exception: pass tracing_id = None @@ -198,14 +193,13 @@ def __call__(self, request): execution_time=time.time() - start_time, added_on=timezone.now() ) - if self.DRF_API_LOGGER_DATABASE: - if LOGGER_THREAD: - d = data.copy() - d['headers'] = json.dumps(d['headers'], indent=4, ensure_ascii=False) if d.get('headers') else '' - if request_data: - d['body'] = json.dumps(d['body'], indent=4, ensure_ascii=False) if d.get('body') else '' - d['response'] = json.dumps(d['response'], indent=4, ensure_ascii=False) if d.get('response') else '' - LOGGER_THREAD.put_log_data(data=d) + if self.DRF_API_LOGGER_DATABASE and LOGGER_THREAD: + d = data.copy() + d['headers'] = json.dumps(d['headers'], indent=4, ensure_ascii=False) if d.get('headers') else '' + if request_data: + d['body'] = json.dumps(d['body'], indent=4, ensure_ascii=False) if d.get('body') else '' + d['response'] = json.dumps(d['response'], indent=4, ensure_ascii=False) if d.get('response') else '' + LOGGER_THREAD.put_log_data(data=d) if self.DRF_API_LOGGER_SIGNAL: if tracing_id: data.update({ diff --git a/drf_api_logger/utils.py b/drf_api_logger/utils.py index 9c4b085..7d9c610 100644 --- a/drf_api_logger/utils.py +++ b/drf_api_logger/utils.py @@ -25,7 +25,7 @@ def get_client_ip(request): else: ip = request.META.get('REMOTE_ADDR') return ip - except: + except Exception: return '' diff --git a/setup.py b/setup.py index 1b44e47..27a5b8e 100644 --- a/setup.py +++ b/setup.py @@ -20,8 +20,8 @@ def get_long_desc(): setuptools.setup( - name="drf_api_logger", - version="1.1.15", + name="drf-api-logger", + version="1.1.16", author="Vishal Anand", author_email="vishalanandl177@gmail.com", description="An API Logger for your Django Rest Framework project.",