4

How do I run django in debug=True without being bombarded by the sql logging?

Being forced to squint through all this noise is making it rather hard to see my debug messages....

[Thu Jul 28 19:56:27 2011] [error] DEBUG:(0.000) SELECT `django_session`.`session_key`, `django_session`.`session_data`, `django_session`.`expire_date` FROM `django_session` WHERE (`django_session`.`session_key` = foobar  AND `django_session`.`expire_date` > 2011-07-28 19:56:27 ); args=('foobar', u'2011-07-28 19:56:27')
[Thu Jul 28 19:56:27 2011] [error] DEBUG:(0.000) SELECT `Users`.`id`, `Users`.`auth_id`, `Users`.`username`, `Users`.`pwhash`, `Users`.`first_name`, `Users`.`last_name`, `Users`.`email`, `Users`.`notes`, `Users`.`force_password_change`, `Users`.`deleted`, `Users`.`deleted_username` FROM `Users` WHERE (`Users`.`deleted` = False  AND `Users`.`id` = 2 ); args=(False, 2)
[Thu Jul 28 19:56:27 2011] [error] DEBUG:(0.000) SELECT `Auths`.`id`, `Auths`.`developer_id`, `Auths`.`application_id` FROM `Auths` WHERE `Auths`.`id` = 2 ; args=(2,)
[Thu Jul 28 19:56:27 2011] [error] DEBUG:(0.000) SELECT `Auths`.`id`, `Auths`.`developer_id`, `Auths`.`application_id` FROM `Auths` WHERE `Auths`.`id` = 2 ; args=(2,)
John Mee
  • 50,179
  • 34
  • 152
  • 186

1 Answers1

6

In Django 1.3 this can be done rather simply with the inclusion of logging. In summary the way I do it is very similar to the docs and keep the focus of logging on my application. My settings.py file currently looks like this.

# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'standard': {
            'format': "[%(asctime)s] %(levelname)s [%(name)s] %(message)s",
            'datefmt': "%d/%b/%Y %H:%M:%S"
        },
    },
    'handlers': {
        'null': {
            'level': 'DEBUG',
            'class': 'django.utils.log.NullHandler',
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'standard'
        },
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
            'propagate': True,
            'level': 'INFO',
        },
        'django.db.backends': {
            'handlers': ['console'],
            'level': 'ERROR',
            'propagate': False,
        },
        'apps': {
            'handlers': ['console'],
            'level': 'DEBUG',
        },
    }
}

If you are using an older version of Django you can setup logging by using filters (Yes you can do this in 1.3 too.) This SO post should provide you with what you need to get going.

Edit Update

Again following the documentation which gives an example combined with this post should get you where you need to go.

  1. Add in a django handler and propagate it down the chain..
  2. Add in a django.db.backends which doesn't propagate but raise the noise floor to ERROR

I don't think there is anything else?

HTH

Community
  • 1
  • 1
rh0dium
  • 6,811
  • 4
  • 46
  • 79
  • Yeah 1.3; better but not ideal: are we not able to isolate them, but have to raise the log level for all 'django' messages to "warning"? – John Mee Jul 29 '11 at 06:00
  • Actually you can - look into propagate. Basically what you want to do is to only log the django.x.y.z you want. What is it you are looking to do? – rh0dium Aug 12 '11 at 20:51