Skip to content

Commit

Permalink
Added QueryLogger Helper
Browse files Browse the repository at this point in the history
  • Loading branch information
promatik committed Jul 20, 2023
1 parent f615ffd commit 5f4c94f
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/FrameworkServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace GemaDigital\Framework;

use DB;
use GemaDigital\Framework\app\Helpers\QueryLogger;
use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Support\ServiceProvider;

class FrameworkServiceProvider extends ServiceProvider
Expand Down Expand Up @@ -31,6 +34,11 @@ public function boot()
$this->bootForConsole();
}

// Log all queries
if (config('app.debug')) {
DB::listen(fn(QueryExecuted $log) => QueryLogger::log($log));
}

// Blade directives

// SVG
Expand Down
34 changes: 30 additions & 4 deletions src/app/Helpers/CommonHelper.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php

use App\Models\User;
use GemaDigital\Framework\app\Helpers\QueryLogger;
use Illuminate\Http\Response;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

if (! function_exists('user')) {
Expand Down Expand Up @@ -175,6 +177,8 @@ function json_response(mixed $data = null, int $code = 0, int $status = 200, mix
)
);

$queries = QueryLogger::list();

$response = array_merge($response, ['debug' => [
'time' => [
'value' => $timeData[0],
Expand All @@ -185,7 +189,11 @@ function json_response(mixed $data = null, int $code = 0, int $status = 200, mix
'unit' => $memoryData[1],
],
'exception' => $exception,
'queries' => DB::getQueryLog(),
'query' => [
'count' => count($queries),
'time' => collect($queries)->pluck('time')->sum(),
'list' => $queries,
],
'post' => request()->request->all(),
]]);
}
Expand Down Expand Up @@ -230,6 +238,25 @@ function json_status(bool $status, int $success = 200, int $fail = 400): Respons
}
}

if (! function_exists('json_response_pagination')) {
function json_response_pagination(mixed $data = null, LengthAwarePaginator $pagination, int $code = 0, int $status = 200, mixed $errors = null, $exception = null): Response
{
$data = [
...$data,
'pagination' => Arr::only($pagination->toArray(), [
'from',
'to',
'total',
'per_page',
'last_page',
'current_page',
]),
];

return json_response($data, $code, $status, $errors, $exception);
}
}

if (! function_exists('sized_image')) {
function sized_image(string $path, int $size): string
{
Expand Down Expand Up @@ -277,8 +304,7 @@ function __call($method, $params)

$signature = $method.crc32(json_encode($params));

return $this->memo[$this->target][$signature]
??=$this->target->$method(...$params);
return $this->memo[$this->target][$signature] ??= $this->target->$method(...$params);
}
};
}
Expand Down
34 changes: 34 additions & 0 deletions src/app/Helpers/QueryLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace GemaDigital\Framework\app\Helpers;

use Illuminate\Database\Events\QueryExecuted;

class QueryLogger
{
private static $queryLogs = [];

/**
* Logs a query
*
* @param QueryExecuted $log
* @return void
*/
public static function log(QueryExecuted $log): void
{
array_push(self::$queryLogs, [
'sql' => vsprintf(str_replace(['%', '?'], ['%%', '%s'], $log->sql), $log->bindings),
'time' => $log->time,
]);
}

/**
* Get query Log
*
* @return array
*/
public static function list(): array
{
return self::$queryLogs;
}
}

0 comments on commit 5f4c94f

Please sign in to comment.