invoiceninja/app/Http/Middleware/QueryLogging.php

78 lines
2 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
*
2021-06-16 16:58:16 +10:00
* @license https://www.elastic.co/licensing/elastic-license
2019-05-11 13:32:07 +10:00
*/
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 Illuminate\Http\Request;
2022-01-30 10:46:39 +11:00
use Illuminate\Support\Facades\DB;
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
2021-10-08 16:29:06 +11:00
// Enable query logging for development
2021-10-13 16:47:53 +11: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
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-09-05 14:03:21 +10:00
2021-10-13 16:47:53 +11:00
// info("Query count = {$count}");
2021-07-09 09:08:20 +10:00
2021-09-05 15:48:15 +10:00
if($count > 175){
2021-09-08 07:05:53 +10:00
nlog("Query count = {$count}");
2021-09-05 09:01:17 +10:00
nlog($queries);
2021-09-05 09:15:21 +10:00
}
2021-08-06 18:02:32 +10:00
2021-06-22 10:51:43 +10:00
$ip = '';
2021-04-29 09:19:00 +10:00
2022-01-30 10:46:39 +11:00
if(request()->hasHeader('Cf-Connecting-Ip'))
$ip = request()->header('Cf-Connecting-Ip');
elseif(request()->hasHeader('X-Forwarded-For'))
2021-06-22 10:51:43 +10:00
$ip = request()->header('Cf-Connecting-Ip');
else{
$ip = request()->ip();
}
LightLogs::create(new DbQuery($request->method(), urldecode($request->url()), $count, $time, $ip))
2021-12-11 20:49:29 +11:00
->queue();
2019-03-26 15:46:08 +11:00
}
2021-04-29 09:19:00 +10:00
2022-03-16 13:06:25 +11:00
return $next($request);
2019-03-26 15:46:08 +11:00
}
}