0001-general-use-connector-logger-in-to_json-25689.patch
passerelle/base/models.py | ||
---|---|---|
5 | 5 |
import logging |
6 | 6 |
import os |
7 | 7 |
import re |
8 |
import sys |
|
9 |
import traceback |
|
8 | 10 |
import base64 |
9 | 11 | |
10 | 12 |
from django.apps import apps |
... | ... | |
533 | 535 |
sourceip = None |
534 | 536 |
attr['sourceip'] = sourceip |
535 | 537 | |
538 |
if kwargs.get('exc_info'): |
|
539 |
(exc_type, exc_value, tb) = sys.exc_info() |
|
540 |
attr['extra']['error_summary'] = traceback.format_exception_only(exc_type, exc_value) |
|
541 | ||
536 | 542 |
ResourceLog.objects.create(**attr) |
537 | 543 | |
538 | 544 |
getattr(self._logger, levelname.lower())(message, *args, **kwargs) |
539 | 545 | |
546 |
def exception(self, message, *args, **kwargs): |
|
547 |
kwargs['exc_info'] = 1 |
|
548 |
self._log('ERROR', message, *args, **kwargs) |
|
549 | ||
540 | 550 |
def debug(self, message, *args, **kwargs): |
541 | 551 |
self._log('DEBUG', message, *args, **kwargs) |
542 | 552 |
passerelle/utils/jsonresponse.py | ||
---|---|---|
41 | 41 | |
42 | 42 | |
43 | 43 |
class to_json(object): |
44 |
def __init__(self, error_code=500, **kwargs): |
|
44 |
def __init__(self, error_code=500, logger=None, **kwargs):
|
|
45 | 45 |
self.error_code = error_code |
46 | 46 |
self.kwargs = kwargs |
47 |
self.logger = logger |
|
47 | 48 |
if 'cls' not in self.kwargs: |
48 | 49 |
self.kwargs['cls'] = JSONEncoder |
49 | 50 | |
... | ... | |
115 | 116 |
return self.api(f, args[1], *args, **kwargs) |
116 | 117 | |
117 | 118 |
def api(self, f, req, *args, **kwargs): |
118 |
logger = logging.getLogger('passerelle.jsonresponse') |
|
119 |
logger = self.logger or logging.getLogger('passerelle.jsonresponse')
|
|
119 | 120 |
try: |
120 | 121 |
resp = f(*args, **kwargs) |
121 | 122 |
if isinstance(resp, HttpResponse): |
... | ... | |
124 | 125 |
data = self.obj_to_response(req, resp) |
125 | 126 |
status = 200 |
126 | 127 |
except Exception as e: |
127 |
extras = {'method': req.method, 'request': req} |
|
128 |
extras = {'method': req.method, 'exception': repr(e)} |
|
129 |
if not self.logger: |
|
130 |
extras['request'] = req |
|
128 | 131 |
if req.method == 'POST': |
129 | 132 |
extras.update({'body': req.body}) |
130 | 133 |
if (not isinstance(e, (Http404, PermissionDenied, ObjectDoesNotExist, RequestException)) |
passerelle/views.py | ||
---|---|---|
356 | 356 |
kwargs['other_params'] = match.kwargs |
357 | 357 |
elif kwargs.get('rest'): |
358 | 358 |
raise Http404() |
359 |
return to_json()(self.perform)(request, *args, **kwargs) |
|
359 |
connector = self.get_object() |
|
360 |
return to_json(logger=connector.logger)(self.perform)(request, *args, **kwargs) |
|
360 | 361 | |
361 | 362 |
def post(self, request, *args, **kwargs): |
362 | 363 |
return self.get(request, *args, **kwargs) |
tests/test_generic_endpoint.py | ||
---|---|---|
96 | 96 |
else: |
97 | 97 |
assert log.message == 'this is an info test' |
98 | 98 | |
99 |
log = ResourceLog.objects.filter(appname='arcgis', slug='test').delete() |
|
100 |
caplog.clear() |
|
99 | 101 |
resp = app.get('/arcgis/test/district', params={'lon': 6.172122, 'lat': 48.673836}, status=200) |
100 | 102 | |
101 |
logger.debug('new token: %s (timeout %ss)', 'hfgjsfg=', 45) |
|
102 | ||
103 | 103 |
# Resource Custom DB Logger |
104 | 104 |
log = ResourceLog.objects.filter(appname='arcgis', slug='test').first() |
105 | 105 |
assert log.appname == 'arcgis' |
tests/test_jsonresponse.py | ||
---|---|---|
123 | 123 |
assert 'err_class' in data |
124 | 124 |
assert 'err' in data |
125 | 125 |
assert data['err'] == 1 |
126 |
assert data['err_class'] == 'test_jsonresponse.CustomException' |
|
126 |
assert data['err_class'] == 'tests.test_jsonresponse.CustomException'
|
|
127 | 127 |
assert result.status_code == 200 |
128 | 128 | |
129 | 129 |
def test_jsonresponse_with_http4O4_exception(): |
tests/test_manager.py | ||
---|---|---|
148 | 148 |
assert 'endpoint GET /csvdatasource/test/query/foobar/?q=toto' in resp.text |
149 | 149 | |
150 | 150 |
resp = resp.click('full page') |
151 |
assert resp.text.count('<td class="timestamp">') == 2 |
|
151 |
assert resp.text.count('<td class="timestamp">') == 4 |
|
152 |
assert resp.text.count('Error occurred while processing request') == 2 |
|
152 | 153 | |
153 | 154 |
resp.form['q'] = 'toto' |
154 | 155 |
resp = resp.form.submit() |
... | ... | |
156 | 157 | |
157 | 158 |
resp.form['q'] = datetime.date.today().strftime('%d/%m/%Y') |
158 | 159 |
resp = resp.form.submit() |
159 |
assert resp.text.count('<td class="timestamp">') == 2
|
|
160 |
assert resp.text.count('<td class="timestamp">') == 4
|
|
160 | 161 | |
161 | 162 |
resp.form['q'] = datetime.date.today().strftime('%d/%m/2010') |
162 | 163 |
resp = resp.form.submit() |
... | ... | |
164 | 165 | |
165 | 166 |
resp.form['q'] = '' |
166 | 167 |
resp = resp.form.submit() |
167 |
assert resp.text.count('<td class="timestamp">') == 2
|
|
168 |
assert resp.text.count('<td class="timestamp">') == 4
|
|
168 | 169 |
log_pk = re.findall(r'data-pk="(.*)"', resp.text)[0] |
169 | 170 |
base_url = re.findall(r'data-log-base-url="(.*)"', resp.text)[0] |
170 | 171 |
resp = app.get(base_url + log_pk + '/') |
171 |
- |