summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Pope <nick@nickpope.me.uk>2021-07-02 14:36:53 +0100
committerGitHub <noreply@github.com>2021-07-02 15:36:53 +0200
commitfa35c8bdbc6aca65d94d6280fa463d5bc7baa5c0 (patch)
treefd32615a8397e8c5068eb45a0c7e43ed1eb44795
parent313c3d1aa14d80922003f841c257ec4e153f8653 (diff)
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>
-rw-r--r--AUTHORS1
-rw-r--r--django/db/backends/utils.py5
-rw-r--r--docs/ref/logging.txt5
-rw-r--r--docs/releases/4.0.txt3
-rw-r--r--tests/backends/tests.py18
5 files changed, 29 insertions, 3 deletions
diff --git a/AUTHORS b/AUTHORS
index 84cbf17c27..6a2224110f 100644
--- a/AUTHORS
+++ b/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>
diff --git a/django/db/backends/utils.py b/django/db/backends/utils.py
index 45c03ba3e9..c342cf79b5 100644
--- a/django/db/backends/utils.py
+++ b/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},
)
diff --git a/docs/ref/logging.txt b/docs/ref/logging.txt
index e60583c41c..8c682dfceb 100644
--- a/docs/ref/logging.txt
+++ b/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.*``
diff --git a/docs/releases/4.0.txt b/docs/releases/4.0.txt
index c1e64c499a..54d0fd4c1d 100644
--- a/docs/releases/4.0.txt
+++ b/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
~~~~~~~~~~~~~~~~~~~
diff --git a/tests/backends/tests.py b/tests/backends/tests.py
index d335aaefbc..1536e93065 100644
--- a/tests/backends/tests.py
+++ b/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):