\DB::listen(function($query) {
$queryTime = $query->time;
$sql = $query->sql;
$bindings = $query->bindings;
});
Jul 16, 2018
10
Level 1
Query log
Does anyone know how to monitor the time of a query? I know there is a query log but my idea is guarded in bd whenever any query exceeds a certain time defined by me
Level 67
i just want create a function to send me a email and save in db every time wich a query was executed
Can you be more specific? For any query that runs? Or a specific query?
If you put this in your AppServiceProviders boot() method, it will monitor all queries. You can take an action if the query time took longer than your threshold.
$maxTime = 30;
\DB::listen(function($query) use ($maxTime) {
if ($query->time > $maxTime) {
// do something... log it, trigger an event, send an email, etc
// log the query being run, the bindings (if any), and how long it took the query
\Log::warning($query->sql, [
'Bindings' => $query->bindings,
'Time' => $query->time
]);
}
});
1 like
Please or to participate in this conversation.