general: use connector logger in to_json() (#25689)
This commit is contained in:
parent
ca58359ad6
commit
5286592cf7
|
@ -5,6 +5,8 @@ import inspect
|
|||
import logging
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import traceback
|
||||
import base64
|
||||
|
||||
from django.apps import apps
|
||||
|
@ -533,10 +535,18 @@ class ProxyLogger(object):
|
|||
sourceip = None
|
||||
attr['sourceip'] = sourceip
|
||||
|
||||
if kwargs.get('exc_info'):
|
||||
(exc_type, exc_value, tb) = sys.exc_info()
|
||||
attr['extra']['error_summary'] = traceback.format_exception_only(exc_type, exc_value)
|
||||
|
||||
ResourceLog.objects.create(**attr)
|
||||
|
||||
getattr(self._logger, levelname.lower())(message, *args, **kwargs)
|
||||
|
||||
def exception(self, message, *args, **kwargs):
|
||||
kwargs['exc_info'] = 1
|
||||
self._log('ERROR', message, *args, **kwargs)
|
||||
|
||||
def debug(self, message, *args, **kwargs):
|
||||
self._log('DEBUG', message, *args, **kwargs)
|
||||
|
||||
|
|
|
@ -41,9 +41,10 @@ class JSONEncoder(DjangoJSONEncoder):
|
|||
|
||||
|
||||
class to_json(object):
|
||||
def __init__(self, error_code=500, **kwargs):
|
||||
def __init__(self, error_code=500, logger=None, **kwargs):
|
||||
self.error_code = error_code
|
||||
self.kwargs = kwargs
|
||||
self.logger = logger
|
||||
if 'cls' not in self.kwargs:
|
||||
self.kwargs['cls'] = JSONEncoder
|
||||
|
||||
|
@ -115,7 +116,7 @@ class to_json(object):
|
|||
return self.api(f, args[1], *args, **kwargs)
|
||||
|
||||
def api(self, f, req, *args, **kwargs):
|
||||
logger = logging.getLogger('passerelle.jsonresponse')
|
||||
logger = self.logger or logging.getLogger('passerelle.jsonresponse')
|
||||
try:
|
||||
resp = f(*args, **kwargs)
|
||||
if isinstance(resp, HttpResponse):
|
||||
|
@ -124,7 +125,9 @@ class to_json(object):
|
|||
data = self.obj_to_response(req, resp)
|
||||
status = 200
|
||||
except Exception as e:
|
||||
extras = {'method': req.method, 'request': req}
|
||||
extras = {'method': req.method, 'exception': repr(e)}
|
||||
if not self.logger:
|
||||
extras['request'] = req
|
||||
if req.method == 'POST':
|
||||
extras.update({'body': req.body})
|
||||
if (not isinstance(e, (Http404, PermissionDenied, ObjectDoesNotExist, RequestException))
|
||||
|
|
|
@ -356,7 +356,8 @@ class GenericEndpointView(GenericConnectorMixin, SingleObjectMixin, View):
|
|||
kwargs['other_params'] = match.kwargs
|
||||
elif kwargs.get('rest'):
|
||||
raise Http404()
|
||||
return to_json()(self.perform)(request, *args, **kwargs)
|
||||
connector = self.get_object()
|
||||
return to_json(logger=connector.logger)(self.perform)(request, *args, **kwargs)
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
return self.get(request, *args, **kwargs)
|
||||
|
|
|
@ -148,7 +148,8 @@ def test_logs(app, admin_user):
|
|||
assert 'endpoint GET /csvdatasource/test/query/foobar/?q=toto' in resp.text
|
||||
|
||||
resp = resp.click('full page')
|
||||
assert resp.text.count('<td class="timestamp">') == 2
|
||||
assert resp.text.count('<td class="timestamp">') == 4
|
||||
assert resp.text.count('Error occurred while processing request') == 2
|
||||
|
||||
resp.form['q'] = 'toto'
|
||||
resp = resp.form.submit()
|
||||
|
@ -156,7 +157,7 @@ def test_logs(app, admin_user):
|
|||
|
||||
resp.form['q'] = datetime.date.today().strftime('%d/%m/%Y')
|
||||
resp = resp.form.submit()
|
||||
assert resp.text.count('<td class="timestamp">') == 2
|
||||
assert resp.text.count('<td class="timestamp">') == 4
|
||||
|
||||
resp.form['q'] = datetime.date.today().strftime('%d/%m/2010')
|
||||
resp = resp.form.submit()
|
||||
|
@ -164,7 +165,7 @@ def test_logs(app, admin_user):
|
|||
|
||||
resp.form['q'] = ''
|
||||
resp = resp.form.submit()
|
||||
assert resp.text.count('<td class="timestamp">') == 2
|
||||
assert resp.text.count('<td class="timestamp">') == 4
|
||||
log_pk = re.findall(r'data-pk="(.*)"', resp.text)[0]
|
||||
base_url = re.findall(r'data-log-base-url="(.*)"', resp.text)[0]
|
||||
resp = app.get(base_url + log_pk + '/')
|
||||
|
|
Loading…
Reference in New Issue