Insane sql logging
Sometimes people ask for strange features, like "I want to log every database query except select".
There will be drawbacks, of course: it will be slower, for example, but they won't care.
It happened to us, and we had to ship this insanity:
import logging
from logging.handlers import RotatingFileHandler
from django.db.backends import BaseDatabaseWrapper
from django.db.models.signals import pre_save, post_save, pre_delete, post_delete
from django.dispatch import receiver
from datetime import datetime
from django.conf import settings
def patch_cursor(self):
""" Monkey Patch BaseDatabaseWrapper to always use the debug cursor """
self.validate_thread_sharing()
return self.make_debug_cursor(self._cursor())
BaseDatabaseWrapper.cursor = patch_cursor
@receiver(pre_delete)
@receiver(pre_save)
def member_pre_save(sender, **kwargs):
l = logging.getLogger('django.db.backends')
l.setLevel(logging.DEBUG)
if len(l.handlers) <= 0:
handler = RotatingFileHandler(settings.BACKUP_FILENAME,
maxBytes=settings.BACKUP_MAXBYTES)
l.addHandler(handler)
l.debug(datetime.now())
@receiver(post_delete)
@receiver(post_save)
def member_post_save(sender, **kwargs):
l = logging.getLogger('django.db.backends')
l.removeHandler(l.handlers[0])
Of course now they told us they want to log the IP of the machine who triggered the query, so we'll have to use a different approach. Sigh.
-- You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/voMGlGJ3UqgJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home