Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log traceback errors on model #18

Merged
merged 9 commits into from
Mar 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions rest_framework_tracking/migrations/0003_add_errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = [
("rest_framework_tracking", "0002_auto_20170118_1713"),
]

operations = [
migrations.AddField(
'APIRequestLog',
'errors',
models.TextField(null=True, blank=True),
),
]
13 changes: 13 additions & 0 deletions rest_framework_tracking/mixins.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .models import APIRequestLog
from django.utils.timezone import now
import traceback


class LoggingMixin(object):
Expand Down Expand Up @@ -71,6 +72,18 @@ def initial(self, request, *args, **kwargs):
finally:
self.request.log.save()

def handle_exception(self, exc):
# basic handling
response = super(LoggingMixin, self).handle_exception(exc)

# log error
self.request.log.errors = traceback.format_exc()
self.request.log.status_code = response.status_code
self.request.log.save()

# return
return response

def finalize_response(self, request, response, *args, **kwargs):
# regular finalize response
response = super(LoggingMixin, self).finalize_response(request, response, *args, **kwargs)
Expand Down
3 changes: 3 additions & 0 deletions rest_framework_tracking/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class BaseAPIRequestLog(models.Model):
# response
response = models.TextField(null=True, blank=True)

# error traceback
errors = models.TextField(null=True, blank=True)

# status code
status_code = models.PositiveIntegerField(null=True, blank=True)

Expand Down
8 changes: 8 additions & 0 deletions tests/test_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@ def test_log_request_404_error(self):
log = APIRequestLog.objects.first()
self.assertEqual(log.status_code, 404)
self.assertIn('Not found', log.response)
self.assertIn('Traceback', log.errors)

def test_log_request_500_error(self):
self.client.get('/500-error-logging')
log = APIRequestLog.objects.first()
self.assertEqual(log.status_code, 500)
self.assertIn('response', log.response)
self.assertIn('Traceback', log.errors)

def test_log_request_415_error(self):
content_type = 'text/plain'
Expand Down
5 changes: 5 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ def test_status_code(self):
status_code=200)
self.assertEqual(log.status_code, 200)

def test_errors(self):
log = APIRequestLog.objects.create(remote_addr=self.ip, requested_at=now(),
errors='dummy')
self.assertEqual(log.errors, 'dummy')

def test_queries_anon(self):
for i in range(100):
APIRequestLog.objects.create(remote_addr=self.ip, requested_at=now())
Expand Down
1 change: 1 addition & 0 deletions tests/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
url(r'^json-logging$', test_views.MockJSONLoggingView.as_view()),
url(r'^validation-error-logging$', test_views.MockValidationErrorLoggingView.as_view()),
url(r'^404-error-logging$', test_views.Mock404ErrorLoggingView.as_view()),
url(r'^500-error-logging$', test_views.Mock500ErrorLoggingView.as_view()),
url(r'^415-error-logging$', test_views.Mock415ErrorLoggingView.as_view()),
url(r'^no-view-log$', test_views.MockNameAPIView.as_view()),
url(r'^view-log$', test_views.MockNameViewSet.as_view({'get': 'list'})),
Expand Down
6 changes: 6 additions & 0 deletions tests/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework import serializers, viewsets, mixins
from rest_framework.exceptions import APIException
from rest_framework_tracking.mixins import LoggingMixin
from rest_framework_tracking.models import APIRequestLog
from tests.test_serializers import ApiRequestLogSerializer
Expand Down Expand Up @@ -71,6 +72,11 @@ def get(self, request):
return get_list_or_404(empty_qs)


class Mock500ErrorLoggingView(LoggingMixin, APIView):
def get(self, request):
raise APIException('response')


class Mock415ErrorLoggingView(LoggingMixin, APIView):
def post(self, request):
return request.data
Expand Down