Refactor query logger into terminate method

This commit is contained in:
David Bomba 2023-05-29 07:58:00 +10:00
parent 2715507a9a
commit df82f176d3
2 changed files with 15 additions and 14 deletions

View File

@ -37,22 +37,25 @@ class QueryLogging
if (! Ninja::isHosted() || ! config('beacon.enabled')) { if (! Ninja::isHosted() || ! config('beacon.enabled')) {
return $next($request); return $next($request);
} }
DB::enableQueryLog();
return $next($request);
}
public function terminate($request, $response)
{
$timeStart = microtime(true); $timeStart = microtime(true);
DB::enableQueryLog();
$response = $next($request);
// hide requests made by debugbar // hide requests made by debugbar
if (strstr($request->url(), '_debugbar') === false) { if (strstr($request->url(), '_debugbar') === false) {
$queries = DB::getQueryLog(); $queries = DB::getQueryLog();
$count = count($queries); $count = count($queries);
$timeEnd = microtime(true); $timeEnd = microtime(true);
$time = $timeEnd - $timeStart; $time = $timeEnd - $timeStart;
// nlog("Query count = {$count}");
// nlog($queries);
// nlog($request->url());
if ($count > 175) { if ($count > 175) {
nlog("Query count = {$count}"); nlog("Query count = {$count}");
nlog($queries); nlog($queries);
@ -60,18 +63,17 @@ class QueryLogging
$ip = ''; $ip = '';
if (request()->hasHeader('Cf-Connecting-Ip')) { if ($request->hasHeader('Cf-Connecting-Ip')) {
$ip = request()->header('Cf-Connecting-Ip'); $ip = $request->header('Cf-Connecting-Ip');
} elseif (request()->hasHeader('X-Forwarded-For')) { } elseif ($request->hasHeader('X-Forwarded-For')) {
$ip = request()->header('Cf-Connecting-Ip'); $ip = $request->header('Cf-Connecting-Ip');
} else { } else {
$ip = request()->ip(); $ip = $request->ip();
} }
LightLogs::create(new DbQuery($request->method(), substr(urldecode($request->url()), 0, 180), $count, $time, $ip)) LightLogs::create(new DbQuery($request->method(), substr(urldecode($request->url()), 0, 180), $count, $time, $ip))
->batch(); ->batch();
} }
return $response;
} }
} }

View File

@ -41,7 +41,6 @@ class CheckGatewayFee implements ShouldQueue
*/ */
public function handle() public function handle()
{ {
nlog("Checking Gateway Fees for Invoice Id = {$this->invoice_id}");
MultiDB::setDb($this->db); MultiDB::setDb($this->db);