77
88import  django .test .testcases 
99from  django .utils .encoding  import  force_str 
10+ from  django .conf  import  settings 
11+ from  django .db .backends .utils  import  logger 
1012
1113from  debug_toolbar .utils  import  get_stack_trace , get_template_info 
1214
1921allow_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+ 
2250class  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