Bläddra i källkod

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>
Nick Pope 3 år sedan
förälder
incheckning
fa35c8bdbc
5 ändrade filer med 29 tillägg och 3 borttagningar
  1. 1 0
      AUTHORS
  2. 3 2
      django/db/backends/utils.py
  3. 5 0
      docs/ref/logging.txt
  4. 2 1
      docs/releases/4.0.txt
  5. 18 0
      tests/backends/tests.py

+ 1 - 0
AUTHORS

@@ -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>

+ 3 - 2
django/db/backends/utils.py

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

+ 5 - 0
docs/ref/logging.txt

@@ -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.*``

+ 2 - 1
docs/releases/4.0.txt

@@ -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
 ~~~~~~~~~~~~~~~~~~~

+ 18 - 0
tests/backends/tests.py

@@ -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):