invoiceninja/app/Http/Middleware/QueryLogging.php

68 lines
1.6 KiB
PHP
Raw Normal View History

2019-03-26 15:46:08 +11:00
<?php
2019-05-11 13:32:07 +10:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-05-11 13:32:07 +10:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2019-05-11 13:32:07 +10:00
*
* @license https://opensource.org/licenses/AAL
*/
2019-03-26 15:46:08 +11:00
namespace App\Http\Middleware;
2021-04-29 09:19:00 +10:00
use App\DataMapper\Analytics\DbQuery;
use App\Utils\Ninja;
2019-03-26 15:46:08 +11:00
use Closure;
use DB;
use Illuminate\Http\Request;
use Log;
2021-04-29 09:19:00 +10:00
use Turbo124\Beacon\Facades\LightLogs;
2019-03-26 15:46:08 +11:00
/**
* Class QueryLogging.
*/
class QueryLogging
{
/**
* Handle an incoming request.
*
* @param Request $request
* @param Closure $next
*
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
2020-09-07 09:29:46 +10:00
2019-03-26 15:46:08 +11:00
// Enable query logging for development
2021-04-29 12:12:44 +10:00
if (!Ninja::isHosted() || !config('beacon.enabled')) {
return $next($request);
}
2020-12-08 20:18:17 +11:00
2021-04-29 09:19:00 +10:00
$timeStart = microtime(true);
2020-12-08 20:18:17 +11:00
DB::enableQueryLog();
2019-03-26 15:46:08 +11:00
$response = $next($request);
2021-04-29 09:19:00 +10:00
// hide requests made by debugbar
if (strstr($request->url(), '_debugbar') === false) {
2019-03-26 15:46:08 +11:00
2021-04-29 09:19:00 +10:00
$queries = DB::getQueryLog();
$count = count($queries);
$timeEnd = microtime(true);
$time = $timeEnd - $timeStart;
2021-04-29 09:19:00 +10:00
nlog($request->method().' - '.$request->url().": $count queries - ".$time);
2021-04-29 09:19:00 +10:00
// if($count > 50)
2021-04-29 11:31:04 +10:00
nlog($queries);
2021-04-29 09:19:00 +10:00
2021-04-29 13:43:40 +10:00
LightLogs::create(new DbQuery($request->method(), $request->url(), $count, $time, request()->ip()))
2021-04-29 09:19:00 +10:00
->batch();
2019-03-26 15:46:08 +11:00
}
2021-04-29 09:19:00 +10:00
2019-03-26 15:46:08 +11:00
return $response;
}
}