Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
adeiming committed Sep 17, 2023
0 parents commit 5f83020
Show file tree
Hide file tree
Showing 13 changed files with 848 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "adeiming/slow-query-logger",
"description": "Reading slow queries and writing in the log",
"license": "MIT",
"autoload": {
"psr-4": {
"Adeiming\\SlowQueryLogger\\": "src/"
}
},
"authors": [
{
"name": "adeiming",
"email": "[email protected]"
}
],
"extra": {
"laravel": {
"providers": [
"Adeiming\\SlowQueryLogger\\SlowQueryLoggerProvider"
]
}
},
"require": {}
}
10 changes: 10 additions & 0 deletions config/slow-query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

/**
* Timeout ini milisecond
*/
return [
'minimum_timeout' => 1,
'maximum_timeout' => 5000,
'log_channel' => 'slow-query',
];
80 changes: 80 additions & 0 deletions src/SlowQueryLoggerProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Adeiming\SlowQueryLogger;

use Carbon\Carbon;
use DateTime;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider;

class SlowQueryLoggerProvider extends ServiceProvider
{
/**
* Register services.
*/
public function register(): void
{
//
}

/**
* Bootstrap services.
*/
public function boot(): void
{
$this->publishes([
__DIR__.'/../config/slow-query.php' => config_path('slow-query.php')
]);

DB::listen(function($query){
$timeout = config('slow-query.minimum_timeout');

if($query->time > $timeout){
$pdo = $query->connection->getPdo();
$data = $query->sql;
foreach ($query->bindings ?? [] as $key => $binding) {
$regex = is_numeric($key)
? "/(?<!\?)\?(?=(?:[^'\\\']*'[^'\\']*')*[^'\\\']*$)(?!\?)/"
: "/:{$key}(?=(?:[^'\\\']*'[^'\\\']*')*[^'\\\']*$)/";

if (!is_int($binding) && !is_float($binding)) {
if ($pdo) {
try {
$binding = $binding instanceof DateTime
? Carbon::parse($binding)->format('Y-m-d H:i:s')
: $binding;

$binding = $pdo->quote((string) $binding);
} catch (\Exception $e) {
$binding = $this->emulateQuote($binding);
}
} else {
$binding = $this->emulateQuote($binding);
}
}

$data = preg_replace($regex, addcslashes($binding, '$'), $data, 1);
}

if ($query->time > $timeout) {
$badge = 'error';
}elseif ($query->time > config('slow-query.maximum_timeout')) {
$badge = 'warning';
}else{
$badge = 'info';
}

Log::channel(config('slow-query.log_channel'))->{$badge}(request()->url(), [$data, $query->time]);
}
});
}

private function emulateQuote($value)
{
$search = ["\\", "\x00", "\n", "\r", "'", '"', "\x1a"];
$replace = ["\\\\","\\0","\\n", "\\r", "\'", '\"', "\\Z"];

return "'" . str_replace($search, $replace, (string) $value) . "'";
}
}
25 changes: 25 additions & 0 deletions vendor/autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

// autoload.php @generated by Composer

if (PHP_VERSION_ID < 50600) {
if (!headers_sent()) {
header('HTTP/1.1 500 Internal Server Error');
}
$err = 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL;
if (!ini_get('display_errors')) {
if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') {
fwrite(STDERR, $err);
} elseif (!headers_sent()) {
echo $err;
}
}
trigger_error(
$err,
E_USER_ERROR
);
}

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInitcd365e1ac8534c6eae55965dab369003::getLoader();
Loading

0 comments on commit 5f83020

Please sign in to comment.