Permalink
Browse files

it actually logs

  • Loading branch information...
va1en0k committed Jun 24, 2011
1 parent dc97ae8 commit 8257f7c6f2def4b6412205e7655c9b025743efa0
@@ -1,2 +1,43 @@
+import datetime
+
+from .models import Request
+
class RequestLoggingMiddleware(object):
- pass
+ def _save_log_entry(self, entry):
+ response_time = datetime.datetime.now() - entry.datetime
+
+ entry.response_time = response_time.total_seconds()
+ entry.save()
+
+ def process_request(self, request):
+ """
+ Creates a request log entry object,
+ ties it to the request
+ """
+
+ entry = Request(method=request.method.upper(),
+ path=request.path,
+ datetime=datetime.datetime.now())
+
+ request.request_log_entry = entry
+
+ def process_response(self, request, response):
+ """
+ Updates and saves a request log entry object
+ """
+ entry = request.request_log_entry
+ entry.status_code = response.status_code
+
+ self._save_log_entry(entry)
+
+ return response
+
+ def process_exception(self, request, exception):
+ entry = request.request_log_entry
+ entry.status_code = 500
+
+ entry.exception_class = exception.__class__.__name__
+ entry.exception_message = exception.message
+
+ self._save_log_entry(entry)
+
View
@@ -0,0 +1,15 @@
+from django.db import models
+
+METHODS = ['GET', 'POST', 'PUT', 'DELETE']
+
+class Request(models.Model):
+ """Request log entry"""
+
+ method = models.CharField(max_length=7, choices=zip(METHODS, METHODS))
+ path = models.CharField(max_length=255)
+ datetime = models.DateTimeField()
+ response_time = models.FloatField()
+ status_code = models.IntegerField()
+ exception_class = models.CharField(max_length=255, blank=True)
+ exception_message = models.TextField(blank=True)
+
@@ -18,7 +18,40 @@ def testItActuallyLogsRequests(self):
req = requests[0]
self.assertEquals(req.method, 'GET')
self.assertEquals(req.path, '/admin/')
- self.assertNotEquals(req.time, 0)
+ self.assertNotEquals(req.response_time, 0)
+ self.assertEquals(req.status_code, 200)
+
+ def testItLogs404(self):
+ self.client.get('/not_exists/')
+
+ requests = Request.objects.all()
+ self.assertEquals(len(requests), 1)
+
+ req = requests[0]
+ self.assertEquals(req.method, 'GET')
+ self.assertEquals(req.path, '/not_exists/')
+ self.assertNotEquals(req.response_time, 0)
+ self.assertEquals(req.status_code, 404)
+
+ def testItLogs500(self):
+ try:
+ self.client.get('/error500/')
+ except NotImplementedError:
+ pass
+
+ requests = Request.objects.all()
+ self.assertEquals(len(requests), 1)
+
+ req = requests[0]
+ self.assertEquals(req.method, 'GET')
+ self.assertEquals(req.path, '/error500/')
+ self.assertNotEquals(req.response_time, 0)
+ self.assertEquals(req.status_code, 500)
+ self.assertEquals(req.exception_class, 'NotImplementedError')
+ self.assertEquals(req.exception_message, '500 error for testing')
+
+
+
View
@@ -103,9 +103,7 @@
ROOT_URLCONF = 'testproject.urls'
TEMPLATE_DIRS = (
- # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
- # Always use forward slashes, even on Windows.
- # Don't forget to use absolute paths, not relative paths.
+ here('templates'),
)
INSTALLED_APPS = (
@@ -0,0 +1 @@
+404
View
@@ -4,7 +4,11 @@
from django.contrib import admin
admin.autodiscover()
+def raise_error(request):
+ raise NotImplementedError('500 error for testing')
+
urlpatterns = patterns('',
+ url(r'^error500/$', raise_error),
# Examples:
# url(r'^$', 'testproject.views.home', name='home'),
# url(r'^testproject/', include('testproject.foo.urls')),
@@ -13,5 +17,6 @@
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
+
url(r'^admin/', include(admin.site.urls)),
)

0 comments on commit 8257f7c

Please sign in to comment.