1
1
import importlib
2
2
import json
3
+ import sys
3
4
import time
4
5
import uuid
5
6
@@ -72,6 +73,16 @@ def __init__(self, get_response):
72
73
mod = importlib .import_module (mod_name )
73
74
self .tracing_func_name = getattr (mod , func_name )
74
75
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
+
75
86
def __call__ (self , request ):
76
87
77
88
# Run only if logger is enabled.
@@ -103,6 +114,12 @@ def __call__(self, request):
103
114
request_data = ''
104
115
try :
105
116
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 = ''
106
123
except :
107
124
pass
108
125
@@ -142,10 +159,13 @@ def __call__(self, request):
142
159
elif getattr (response , 'streaming' , False ):
143
160
response_body = '** Streaming **'
144
161
else :
145
- if type (response .content ) == bytes :
162
+ if type (response .content ) is bytes :
146
163
response_body = json .loads (response .content .decode ())
147
164
else :
148
165
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 = ''
149
169
if self .DRF_API_LOGGER_PATH_TYPE == 'ABSOLUTE' :
150
170
api = request .build_absolute_uri ()
151
171
elif self .DRF_API_LOGGER_PATH_TYPE == 'FULL_PATH' :
@@ -169,10 +189,10 @@ def __call__(self, request):
169
189
if self .DRF_API_LOGGER_DATABASE :
170
190
if LOGGER_THREAD :
171
191
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 ''
173
193
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 ''
176
196
LOGGER_THREAD .put_log_data (data = d )
177
197
if self .DRF_API_LOGGER_SIGNAL :
178
198
if tracing_id :
0 commit comments