Log django database queries using logging

109 views
Skip to first unread message

Anju SB

unread,
Mar 13, 2014, 3:18:30 AM3/13/14
to django...@googlegroups.com
Dear All,    

  I  am a newbie in Django programming. I want to log insert, update, delete and failed select query in my daily log file. I configured 'django.db.backends' in the logger of the Logging dict in Settings.py. But I get all the queries in that application. I need only insert, update, delete and failed select queries.

Please help me

Thanking you

 Anju

Russell Keith-Magee

unread,
Mar 13, 2014, 10:55:38 PM3/13/14
to Django Users
Hi Anju,

Firstly - if you want to filter log messages that meet a specific criterion, you can install a filter into your logging configuration. Python and Django's logging documentation both contain sections about filtering:


Secondly - I'd be questioning whether Django is the right place to be logging this. Your database also has logging capabilities; I'd be looking to see whether you can configure your database to produce the logs you need.

Yours,
Russ Magee %-)



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4b34ac77-5a38-46e1-875e-f74c600c7e08%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Anju SB

unread,
Mar 14, 2014, 1:52:36 AM3/14/14
to django...@googlegroups.com

Thank You Mr. Russell Keith-Magee.

I want to log other details regarding the application.  So I selected the django logging.  

LOG_URL =  '/var/log/Crime_Mapping/'
 
import logging

class CustomQueryFilter(logging.Filter):
    def filter(self, record):
        for action in ['INSERT', 'UPDATE', 'DELETE']:
            if action in getattr(record, 'sql', ''):
                return True
        return False
 

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse',
        },
'db_query_filter': {
            '()': CustomQueryFilter,
        },
    },
    'formatters': {
        'verbose': {
            'format' : "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(module)s %(process)d %(thread)d %(message)s",
            'datefmt' : "%d/%b/%Y %H:%M:%S"
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        },
'file':{
   'level': 'DEBUG',
   'filters': ['db_query_filter'],
   'class': 'logging.FileHandler',
   'filename': LOG_URL+'crime_log.log',
   'formatter': 'verbose'
},

    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
'my_logger': {
            'handlers': ['file'],
            'level': 'DEBUG',
        },
        'django.db': {
            'level': 'DEBUG',
            'handers': ['file'],
   'filters': ['db_query_filter'],
   'propagate': True,
        },

    }


But i get all queries in my log file
Reply all
Reply to author
Forward
0 new messages