1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-13 22:54:25 +01:00
invoiceninja/app/Http/Middleware/QueryLogging.php

78 lines
1.8 KiB
PHP
Raw Normal View History

2019-03-26 05:46:08 +01:00
<?php
2019-05-11 05:32:07 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-05-11 05:32:07 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2019-05-11 05:32:07 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2019-05-11 05:32:07 +02:00
*/
2019-03-26 05:46:08 +01:00
namespace App\Http\Middleware;
2021-04-29 01:19:00 +02:00
use App\DataMapper\Analytics\DbQuery;
use App\Utils\Ninja;
2019-03-26 05:46:08 +01:00
use Closure;
use DB;
use Illuminate\Http\Request;
use Log;
2021-04-29 01:19:00 +02:00
use Turbo124\Beacon\Facades\LightLogs;
2019-03-26 05:46:08 +01: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 01:29:46 +02:00
2021-10-08 07:29:06 +02:00
// Enable query logging for development
2021-10-13 07:47:53 +02:00
if (!Ninja::isHosted() || !config('beacon.enabled')) {
return $next($request);
}
2020-12-08 10:18:17 +01:00
2021-04-29 01:19:00 +02:00
$timeStart = microtime(true);
2020-12-08 10:18:17 +01:00
DB::enableQueryLog();
2019-03-26 05:46:08 +01:00
$response = $next($request);
2021-04-29 01:19:00 +02:00
// hide requests made by debugbar
if (strstr($request->url(), '_debugbar') === false) {
2019-03-26 05:46:08 +01:00
2021-04-29 01:19:00 +02:00
$queries = DB::getQueryLog();
$count = count($queries);
$timeEnd = microtime(true);
$time = $timeEnd - $timeStart;
2021-09-05 06:03:21 +02:00
2021-10-13 07:47:53 +02:00
// info("Query count = {$count}");
2021-07-09 01:08:20 +02:00
2021-09-05 07:48:15 +02:00
if($count > 175){
2021-09-07 23:05:53 +02:00
nlog("Query count = {$count}");
2021-09-05 01:01:17 +02:00
nlog($queries);
2021-09-05 01:15:21 +02:00
}
2021-08-06 10:02:32 +02:00
2021-06-22 02:51:43 +02:00
$ip = '';
2021-04-29 01:19:00 +02:00
2021-06-22 02:51:43 +02:00
if(request()->header('Cf-Connecting-Ip'))
$ip = request()->header('Cf-Connecting-Ip');
else{
$ip = request()->ip();
}
LightLogs::create(new DbQuery($request->method(), urldecode($request->url()), $count, $time, $ip))
2021-04-29 01:19:00 +02:00
->batch();
2019-03-26 05:46:08 +01:00
}
2021-04-29 01:19:00 +02:00
2019-03-26 05:46:08 +01:00
return $response;
}
}