Skip to content

Commit 7f9f61c

Browse files
committed
Subclass OperationDebugWrapper to log to info
1 parent f098236 commit 7f9f61c

File tree

1 file changed

+29
-1
lines changed
  • django_mongodb_extensions/debug_toolbar/panels/mql

1 file changed

+29
-1
lines changed

django_mongodb_extensions/debug_toolbar/panels/mql/tracking.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import django.test.testcases
99
from django.utils.encoding import force_str
10+
from django.conf import settings
11+
from django.db.backends.utils import logger
1012

1113
from debug_toolbar.utils import get_stack_trace, get_template_info
1214

@@ -19,6 +21,32 @@
1921
allow_sql = contextvars.ContextVar("debug-toolbar-allow-sql", default=True)
2022

2123

24+
25+
class DebugToolbarWrapper(OperationDebugWrapper):
26+
def log(self, op, duration, args, kwargs=None):
27+
msg = "(%.3f) %s"
28+
args = ", ".join(repr(arg) for arg in args)
29+
operation = f"db.{self.collection_name}{op}({args})"
30+
if len(settings.DATABASES) > 1:
31+
msg += f"; alias={self.db.alias}"
32+
self.db.queries_log.append(
33+
{
34+
"sql": operation,
35+
"time": "%.3f" % duration,
36+
}
37+
)
38+
logger.info(
39+
msg,
40+
duration,
41+
operation,
42+
extra={
43+
"duration": duration,
44+
"sql": operation,
45+
"alias": self.db.alias,
46+
},
47+
)
48+
49+
2250
class SQLQueryTriggered(Exception):
2351
"""Thrown when template panel triggers a query"""
2452

@@ -27,7 +55,7 @@ def patch_get_collection(connection):
2755

2856
def get_collection(self, name, **kwargs):
2957
collection = Collection(self.database, name, **kwargs)
30-
collection = OperationDebugWrapper(self, collection)
58+
collection = DebugToolbarWrapper(self, collection)
3159
return collection
3260

3361
if not hasattr(connection, "_djdt_cursor"):

0 commit comments

Comments
 (0)