Browse Source

Add USE_LOGGING setting which will disable direct error creation in the middleware, and send it to the the dblog logger instead

David Cramer 14 years ago
parent
commit
f52d9891ce
4 changed files with 29 additions and 10 deletions
  1. 9 1
      README.rst
  2. 2 2
      djangodblog/manager.py
  3. 15 6
      djangodblog/middleware.py
  4. 3 1
      djangodblog/settings.py

+ 9 - 1
README.rst

@@ -26,7 +26,7 @@ Review the diff, then make any changes which appear necessary.
 Notable Changes
 ###############
 
-* 2.0 Added `checksum` column to Error. Several indexes were created. Checksum calculation slightly changed.
+* 2.0.0 Added `checksum` column to Error. Several indexes were created. Checksum calculation slightly changed.
 * 1.4.0 Added `logger` column to both Error and ErrorBatch. `traceback` and `class_name` are now nullable.
 * 1.3.0 Added `level` column to both Error and ErrorBatch.
 
@@ -111,6 +111,14 @@ Enables showing full embedded (enhanced) tracebacks within the administration fo
 
 * Note: Even if you disable displaying of enhanced tracebacks, dblog will still store the entire exception stacktrace.
 
+#############
+DBLOG_LOGGING
+#############
+
+Enabling this setting will turn off automatic database logging with the middleware, and instead send all exceptions to the named logger ``dblog``. Use this in conjuction with ``djangodblog.handlers.DBLogHandler`` or your own handler to tweak how logging is dealt with.
+
+A good example use case for this, is if you want to write to something like a syslog ahead of time, and later process that into the database with another tool.
+
 ############################
 Integration with ``logging``
 ############################

+ 2 - 2
djangodblog/manager.py

@@ -31,8 +31,8 @@ class DBLogManager(models.Manager):
 
     def get_query_set(self):
         qs = super(DBLogManager, self).get_query_set()
-        if DBLOG_DATABASE_USING:
-            qs = qs.using(DBLOG_DATABASE_USING)
+        if DATABASE_USING:
+            qs = qs.using(DATABASE_USING)
         return qs
 
     def _create(self, **defaults):

+ 15 - 6
djangodblog/middleware.py

@@ -4,6 +4,8 @@ from django.http import Http404
 from djangodblog.models import Error
 from djangodblog.settings import *
 
+import logging
+
 __all__ = ('DBLogMiddleware',)
 
 class DBLogMiddleware(object):
@@ -19,9 +21,16 @@ class DBLogMiddleware(object):
         if transaction.is_dirty():
             transaction.rollback()
 
-        Error.objects.create_from_exception(url=request.build_absolute_uri(), data=dict(
-            META=request.META,
-            POST=request.POST,
-            GET=request.GET,
-            COOKIES=request.COOKIES,
-        ))
+        extra = dict(
+            url=request.build_absolute_uri(), data=dict(
+                META=request.META,
+                POST=request.POST,
+                GET=request.GET,
+                COOKIES=request.COOKIES,
+            )
+        )
+
+        if USE_LOGGING:
+            logging.getLogger('dblog').exception(exception, extra=extra)
+        else:
+            Error.objects.create_from_exception(**extra)        

+ 3 - 1
djangodblog/settings.py

@@ -4,4 +4,6 @@ CATCH_404_ERRORS = getattr(settings, 'DBLOG_CATCH_404_ERRORS', False)
 
 ENHANCED_TRACEBACKS = getattr(settings, 'DBLOG_ENHANCED_TRACEBACKS', True)
 
-DATABASE_USING = getattr(settings, 'DBLOG_DATABASE_USING', None)
+DATABASE_USING = getattr(settings, 'DBLOG_DATABASE_USING', None)
+
+USE_LOGGING = getattr(settings, 'DBLOG_USE_LOGGING', False)