-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5f83020
Showing
13 changed files
with
848 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Auto detect text files and perform LF normalization | ||
* text=auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) . "'"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Oops, something went wrong.