1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Fixed #26157 #25321 -- Added sql/params to extra context of schema logger

Thanks Akshesh Doshi for the initial patch and Tim Graham for the review
This commit is contained in:
Markus Holtermann 2016-03-14 21:38:38 +11:00
parent d0fe6c9156
commit 1cb65b8a77
4 changed files with 32 additions and 3 deletions

View File

@ -99,7 +99,7 @@ class BaseDatabaseSchemaEditor(object):
Executes the given SQL statement, with optional parameters. Executes the given SQL statement, with optional parameters.
""" """
# Log the command we're running, then run it # Log the command we're running, then run it
logger.debug("%s; (params %r)", sql, params) logger.debug("%s; (params %r)", sql, params, extra={'params': params, 'sql': sql})
if self.collect_sql: if self.collect_sql:
ending = "" if sql.endswith(";") else ";" ending = "" if sql.endswith(";") else ";"
if params is not None: if params is not None:

View File

@ -502,7 +502,7 @@ class ignore_warnings(TestContextDecorator):
@contextmanager @contextmanager
def patch_logger(logger_name, log_level): def patch_logger(logger_name, log_level, log_kwargs=False):
""" """
Context manager that takes a named logger and the logging level Context manager that takes a named logger and the logging level
and provides a simple mock-like list of messages received and provides a simple mock-like list of messages received
@ -510,7 +510,8 @@ def patch_logger(logger_name, log_level):
calls = [] calls = []
def replacement(msg, *args, **kwargs): def replacement(msg, *args, **kwargs):
calls.append(msg % args) call = msg % args
calls.append((call, kwargs) if log_kwargs else call)
logger = logging.getLogger(logger_name) logger = logging.getLogger(logger_name)
orig = getattr(logger, log_level) orig = getattr(logger, log_level)
setattr(logger, log_level, replacement) setattr(logger, log_level, replacement)

View File

@ -583,6 +583,13 @@ specific logger following this example:
Logs the SQL queries that are executed during schema changes to the database by Logs the SQL queries that are executed during schema changes to the database by
the :doc:`migrations framework </topics/migrations>`. Note that it won't log the the :doc:`migrations framework </topics/migrations>`. Note that it won't log the
queries executed by :class:`~django.db.migrations.operations.RunPython`. queries executed by :class:`~django.db.migrations.operations.RunPython`.
Messages to this logger have ``params`` and ``sql`` in their extra context (but
unlike ``django.db.backends``, not duration). The values have the same meaning
as explained in :ref:`django-db-logger`.
.. versionadded:: 1.10
The ``extra`` context was added.
Handlers Handlers
-------- --------

View File

@ -9,6 +9,7 @@ from admin_scripts.tests import AdminScriptTestCase
from django.conf import settings from django.conf import settings
from django.core import mail from django.core import mail
from django.core.files.temp import NamedTemporaryFile from django.core.files.temp import NamedTemporaryFile
from django.db import connection
from django.test import RequestFactory, SimpleTestCase, override_settings from django.test import RequestFactory, SimpleTestCase, override_settings
from django.test.utils import LoggingCaptureMixin, patch_logger from django.test.utils import LoggingCaptureMixin, patch_logger
from django.utils.deprecation import RemovedInNextVersionWarning from django.utils.deprecation import RemovedInNextVersionWarning
@ -475,3 +476,23 @@ format=%(message)s
out, err = self.run_manage(['check']) out, err = self.run_manage(['check'])
self.assertNoOutput(err) self.assertNoOutput(err)
self.assertOutput(out, "System check identified no issues (0 silenced).") self.assertOutput(out, "System check identified no issues (0 silenced).")
class SchemaLoggerTests(SimpleTestCase):
def test_extra_args(self):
editor = connection.schema_editor(collect_sql=True)
sql = "SELECT * FROM foo WHERE id in (%s, %s)"
params = [42, 1337]
with patch_logger('django.db.backends.schema', 'debug', log_kwargs=True) as logger:
editor.execute(sql, params)
self.assertEqual(
logger,
[(
'SELECT * FROM foo WHERE id in (%s, %s); (params [42, 1337])',
{'extra': {
'sql': 'SELECT * FROM foo WHERE id in (%s, %s)',
'params': [42, 1337],
}},
)]
)