Fixed #30934 -- Included database alias in django.db.backends log messages.

This is useful when working with database routing as you want to know
where each query is being executed.

Co-authored-by: David Winterbottom <david.winterbottom@gmail.com>
This commit is contained in:
Nick Pope 2021-07-02 14:36:53 +01:00 committed by GitHub
parent 313c3d1aa1
commit fa35c8bdbc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 3 deletions

View File

@ -251,6 +251,7 @@ answer newbie questions, and generally made Django that much better:
David Sanders <dsanders11@ucsbalum.com>
David Schein
David Tulig <david.tulig@gmail.com>
David Winterbottom <david.winterbottom@gmail.com>
David Wobrock <david.wobrock@gmail.com>
Davide Ceretti <dav.ceretti@gmail.com>
Deep L. Sukhwani <deepsukhwani@gmail.com>

View File

@ -121,11 +121,12 @@ class CursorDebugWrapper(CursorWrapper):
'time': '%.3f' % duration,
})
logger.debug(
'(%.3f) %s; args=%s',
'(%.3f) %s; args=%s; alias=%s',
duration,
sql,
params,
extra={'duration': duration, 'sql': sql, 'params': params},
self.db.alias,
extra={'duration': duration, 'sql': sql, 'params': params, 'alias': self.db.alias},
)

View File

@ -178,6 +178,7 @@ Messages to this logger have the following extra context:
* ``duration``: The time taken to execute the SQL statement.
* ``sql``: The SQL statement that was executed.
* ``params``: The parameters that were used in the SQL call.
* ``alias``: The alias of the database used in the SQL call.
For performance reasons, SQL logging is only enabled when
``settings.DEBUG`` is set to ``True``, regardless of the logging
@ -188,6 +189,10 @@ This logging does not include framework-level initialization (e.g.
``COMMIT``, and ``ROLLBACK``). Turn on query logging in your database if you
wish to view all database queries.
.. versionchanged:: 4.0
The database ``alias`` was added to log messages.
.. _django-security-logger:
``django.security.*``

View File

@ -226,7 +226,8 @@ Internationalization
Logging
~~~~~~~
* ...
* The alias of the database used in an SQL call is now passed as extra context
along with each message to the :ref:`django-db-logger` logger.
Management Commands
~~~~~~~~~~~~~~~~~~~

View File

@ -3,6 +3,7 @@ import datetime
import threading
import unittest
import warnings
from unittest import mock
from django.core.management.color import no_style
from django.db import (
@ -491,6 +492,23 @@ class BackendTestCase(TransactionTestCase):
BaseDatabaseWrapper.queries_limit = old_queries_limit
new_connection.close()
@mock.patch('django.db.backends.utils.logger')
@override_settings(DEBUG=True)
def test_queries_logger(self, mocked_logger):
sql = 'SELECT 1' + connection.features.bare_select_suffix
with connection.cursor() as cursor:
cursor.execute(sql)
params, kwargs = mocked_logger.debug.call_args
self.assertIn('; alias=%s', params[0])
self.assertEqual(params[2], sql)
self.assertEqual(params[3], None)
self.assertEqual(params[4], connection.alias)
self.assertEqual(
list(kwargs['extra']),
['duration', 'sql', 'params', 'alias'],
)
self.assertEqual(tuple(kwargs['extra'].values()), params[1:])
def test_timezone_none_use_tz_false(self):
connection.ensure_connection()
with self.settings(TIME_ZONE=None, USE_TZ=False):