0

The database configuration is as follows:

https://dev.mysql.com/doc/connector-python/en/connector-python-django-backend.html

As the title?

Previously django.db.backends, I added the following configuration in setting.py, print execution SQL statement。

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console':{
            'level':'DEBUG',
            'class':'logging.StreamHandler',
        },
    },
    'loggers': {
        'django.db.backends': {
            'handlers': ['console'],
            'propagate': True,
            'level':'DEBUG',
        },
    }
}

1 Answers1

0

The reason why your logging configuration doesn't work just like that is because, most likely, your settings.DEBUG = False.

This is something that is stated in the Django docs, at this section, where it says (emphasis mine):

For performance reasons, SQL logging is only enabled when settings.DEBUG is set to True, regardless of the logging level or handlers that are installed.

So I assume you don't want to see every SQL query in production, so change settings.DEBUG to True, in development mode and it will work, I just gave it a try. Make sure your DEBUG attribute is not overridden by some env_var or by some other mechanism. The last value of settings.DEBUG should be True in order for this to work. And again, do this only in development mode.

The reason why messing with the logging settings doesn't work is because the settings.DEBUG attribute controls whether Django uses a CursorDebugWrapper (which logs all queries it executes), instead of a CursorWrapper (which does not). This happens here.

If you want it to work with settings.DEBUG = False (not recommended), then see this answer on how to force the DB to use the CursorDebugWrapper.


Marius Mucenicu
  • 1,685
  • 2
  • 16
  • 25