How to stop sql logging in debug mode?

2019-02-19 02:15发布

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,)

1条回答
三岁会撩人
2楼-- · 2019-02-19 02:27

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

查看更多
登录 后发表回答