Skip to content

Commit 2cadf57

Browse files
<Vishal> Now ignore data based on Maximum request and response body size.
1 parent dd4d1dc commit 2cadf57

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

drf_api_logger/middleware/api_logger_middleware.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import importlib
22
import json
3+
import sys
34
import time
45
import uuid
56

@@ -72,6 +73,16 @@ def __init__(self, get_response):
7273
mod = importlib.import_module(mod_name)
7374
self.tracing_func_name = getattr(mod, func_name)
7475

76+
self.DRF_API_LOGGER_MAX_REQUEST_BODY_SIZE = -1
77+
if hasattr(settings, 'DRF_API_LOGGER_MAX_REQUEST_BODY_SIZE'):
78+
if type(settings.DRF_API_LOGGER_MAX_REQUEST_BODY_SIZE) is int:
79+
self.DRF_API_LOGGER_MAX_REQUEST_BODY_SIZE = settings.DRF_API_LOGGER_MAX_REQUEST_BODY_SIZE
80+
81+
self.DRF_API_LOGGER_MAX_RESPONSE_BODY_SIZE = -1
82+
if hasattr(settings, 'DRF_API_LOGGER_MAX_RESPONSE_BODY_SIZE'):
83+
if type(settings.DRF_API_LOGGER_MAX_RESPONSE_BODY_SIZE) is int:
84+
self.DRF_API_LOGGER_MAX_RESPONSE_BODY_SIZE = settings.DRF_API_LOGGER_MAX_RESPONSE_BODY_SIZE
85+
7586
def __call__(self, request):
7687

7788
# Run only if logger is enabled.
@@ -103,6 +114,12 @@ def __call__(self, request):
103114
request_data = ''
104115
try:
105116
request_data = json.loads(request.body) if request.body else ''
117+
if self.DRF_API_LOGGER_MAX_REQUEST_BODY_SIZE > -1:
118+
if sys.getsizeof(request_data) > self.DRF_API_LOGGER_MAX_REQUEST_BODY_SIZE:
119+
"""
120+
Ignore the request body if larger then specified.
121+
"""
122+
request_data = ''
106123
except:
107124
pass
108125

@@ -142,10 +159,13 @@ def __call__(self, request):
142159
elif getattr(response, 'streaming', False):
143160
response_body = '** Streaming **'
144161
else:
145-
if type(response.content) == bytes:
162+
if type(response.content) is bytes:
146163
response_body = json.loads(response.content.decode())
147164
else:
148165
response_body = json.loads(response.content)
166+
if self.DRF_API_LOGGER_MAX_RESPONSE_BODY_SIZE > -1:
167+
if sys.getsizeof(response_body) > self.DRF_API_LOGGER_MAX_RESPONSE_BODY_SIZE:
168+
response_body = ''
149169
if self.DRF_API_LOGGER_PATH_TYPE == 'ABSOLUTE':
150170
api = request.build_absolute_uri()
151171
elif self.DRF_API_LOGGER_PATH_TYPE == 'FULL_PATH':
@@ -169,10 +189,10 @@ def __call__(self, request):
169189
if self.DRF_API_LOGGER_DATABASE:
170190
if LOGGER_THREAD:
171191
d = data.copy()
172-
d['headers'] = json.dumps(d['headers'], indent=4, ensure_ascii=False)
192+
d['headers'] = json.dumps(d['headers'], indent=4, ensure_ascii=False) if d.get('headers') else ''
173193
if request_data:
174-
d['body'] = json.dumps(d['body'], indent=4, ensure_ascii=False)
175-
d['response'] = json.dumps(d['response'], indent=4, ensure_ascii=False)
194+
d['body'] = json.dumps(d['body'], indent=4, ensure_ascii=False) if d.get('body') else ''
195+
d['response'] = json.dumps(d['response'], indent=4, ensure_ascii=False) if d.get('response') else ''
176196
LOGGER_THREAD.put_log_data(data=d)
177197
if self.DRF_API_LOGGER_SIGNAL:
178198
if tracing_id:

0 commit comments

Comments
 (0)