@@ -65,6 +65,13 @@ class DebugLog extends AbstractLogger
6565 */
6666 protected bool $ _includeSchema = false ;
6767
68+ /**
69+ * Whether a transaction is currently open or not.
70+ *
71+ * @var bool
72+ */
73+ protected bool $ inTransaction = false ;
74+
6875 /**
6976 * Constructor
7077 *
@@ -127,6 +134,7 @@ public function totalTime(): float
127134 */
128135 public function log ($ level , string |Stringable $ message , array $ context = []): void
129136 {
137+ /** @var \Cake\Database\Log\LoggedQuery|object|null $query */
130138 $ query = $ context ['query ' ] ?? null ;
131139
132140 if ($ this ->_logger ) {
@@ -146,6 +154,9 @@ public function log($level, string|Stringable $message, array $context = []): vo
146154 ], JSON_PRETTY_PRINT ),
147155 'took ' => $ took ,
148156 'rows ' => $ context ['response ' ]['hits ' ]['total ' ]['value ' ] ?? $ context ['response ' ]['hits ' ]['total ' ] ?? 0 ,
157+ 'inTransaction ' => $ this ->inTransaction ,
158+ 'isCommitOrRollback ' => false ,
159+ 'role ' => '' ,
149160 ];
150161
151162 return ;
@@ -162,11 +173,26 @@ public function log($level, string|Stringable $message, array $context = []): vo
162173
163174 $ this ->_totalTime += $ data ['took ' ];
164175
176+ $ sql = (string )$ query ;
177+ $ isBegin = $ sql === 'BEGIN ' ;
178+ $ isCommitOrRollback = $ sql === 'COMMIT ' || $ sql === 'ROLLBACK ' ;
179+
180+ if ($ isBegin ) {
181+ $ this ->inTransaction = true ;
182+ }
183+
165184 $ this ->_queries [] = [
166- 'query ' => ( string ) $ query ,
185+ 'query ' => $ sql ,
167186 'took ' => $ data ['took ' ],
168187 'rows ' => $ data ['numRows ' ],
188+ 'inTransaction ' => $ this ->inTransaction ,
189+ 'isCommitOrRollback ' => $ isCommitOrRollback ,
190+ 'role ' => $ query ->getContext ()['role ' ],
169191 ];
192+
193+ if ($ isCommitOrRollback ) {
194+ $ this ->inTransaction = false ;
195+ }
170196 }
171197
172198 /**
0 commit comments