Skip to content

Commit

Permalink
Upgrade to all laravel versions to 9.x
Browse files Browse the repository at this point in the history
A few tweaks made to suit our needs.
May move the threashold to micro-seconds in a later release.
  • Loading branch information
judgej committed Jun 17, 2022
1 parent dac7b69 commit 6c02f5c
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 105 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
composer require rokde/laravel-slow-query-logger
```

Look into your `laravel.log` file to see your messy queries.
Look into your `laravel.log` file to see your slow queries.

## Installation

Add to your composer.json following lines

"require": {
"rokde/laravel-slow-query-logger": "^1.*"
}
"require": {
"rokde/laravel-slow-query-logger": "^1.*"
}

Run `php artisan vendor:publish --provider="Rokde\LaravelSlowQueryLogger\LaravelSlowQueryLoggerProvider"`

Expand All @@ -34,18 +34,19 @@ Sets the channel to log in.

You can set this value through environment variable `LARAVEL_SLOW_QUERY_LOGGER_CHANNEL`. It is `single` by default.

### `log-level`
### `level`

Set the log-level for logging the slow queries.

You can set this value through environment variable `LARAVEL_SLOW_QUERY_LOGGER_LOG_LEVEL`. It is `debug` by default.
You can set this value through environment variable `LARAVEL_SLOW_QUERY_LOGGER_LEVEL`. It is `debug` by default.

### `time-to-log`
### `threshold-ms`

Only log queries longer than this value in microseconds.
Only log queries longer than this value in milliseconds.

You can set this value through environment variable `LARAVEL_SLOW_QUERY_LOGGER_TIME_TO_LOG`. It is `0.7` by default.
You can set this value through environment variable `LARAVEL_SLOW_QUERY_LOGGER_THRESHOLD`. It is `700` by default.
A value of 0 will log all queries.

## Usage

Start your application and look into your logs to identify the slow queries.
Start your application and look into your logs to identify the slow queries.
53 changes: 26 additions & 27 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
{
"name": "rokde/laravel-slow-query-logger",
"description": "Slow Query Logger for Laravel",
"license": "MIT",
"authors": [
{
"name": "Robert Kummer",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"Rokde\\LaravelSlowQueryLogger\\": "src/"
}
},
"minimum-stability": "stable",
"require": {
"php": "^7.1.3",
"illuminate/database": "^5.6",
"illuminate/support": "^5.6"
},
"extra": {
"laravel": {
"providers": [
"Rokde\\LaravelSlowQueryLogger\\LaravelSlowQueryLoggerProvider"
]
}
}
"name": "rokde/laravel-slow-query-logger",
"description": "Slow Query Logger for Laravel",
"license": "MIT",
"authors": [
{
"name": "Robert Kummer",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"Rokde\\LaravelSlowQueryLogger\\": "src/"
}
},
"minimum-stability": "stable",
"require": {
"illuminate/database": "^5.6|^7.0|^8.0|^9.0",
"illuminate/support": "^5.6|^7.0|^8.0|^9.0"
},
"extra": {
"laravel": {
"providers": [
"Rokde\\LaravelSlowQueryLogger\\LaravelSlowQueryLoggerProvider"
]
}
}
}
43 changes: 26 additions & 17 deletions config/slow-query-logger.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
<?php

return [
/**
* log when you are on these environments
*/
'enabled' => env('LARAVEL_SLOW_QUERY_LOGGER_ENABLED', false),
/**
* log when you are on these environments
*/
'enabled' => env('LARAVEL_SLOW_QUERY_LOGGER_ENABLED', false),

/**
* log when you are on these environments
*/
'channel' => env('LARAVEL_SLOW_QUERY_LOGGER_CHANNEL', 'single'),
/**
* log when you are on these environments
*/
'channel' => env('LARAVEL_SLOW_QUERY_LOGGER_CHANNEL', 'single'),

/**
* level to log
*/
'log-level' => env('LARAVEL_SLOW_QUERY_LOGGER_LOG_LEVEL', 'debug'),
/**
* Log level to use.
*/
'level' => env('LARAVEL_SLOW_QUERY_LOGGER_LEVEL', 'debug'),

/**
* log all sql queries that are slower than X seconds
* laravel measures at a precision of 2 digits, so 0.7134 will be logged as 0.71
*/
'time-to-log' => env('LARAVEL_SLOW_QUERY_LOGGER_TIME_TO_LOG', 0.7),
/**
* Log the bindings separately in the context.
*/
'show-bindings' => env('LARAVEL_SLOW_QUERY_LOGGER_SHOW_BINDINGS', false),

/**
* Substitute the bindings in the placeholders of the query.
*/
'replace-bindings' => env('LARAVEL_SLOW_QUERY_LOGGER_REPLACE_BINDINGS', false),

/**
* log all sql queries that are slower than a threashold milli-seconds
*/
'threshold-ms' => env('LARAVEL_SLOW_QUERY_LOGGER_THRESHOLD_MS', 700),
];
115 changes: 64 additions & 51 deletions src/LaravelSlowQueryLoggerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,82 @@

namespace Rokde\LaravelSlowQueryLogger;

use Exception;
use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider;
use Throwable;

class LaravelSlowQueryLoggerProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*/
public function boot()
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__ . '/../config/slow-query-logger.php' => config_path('slow-query-logger.php'),
], 'config');
}
/**
* Bootstrap the application services.
*/
public function boot()
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__ . '/../config/slow-query-logger.php' => config_path('slow-query-logger.php'),
], 'config');
}

$this->setupListener();
}
$this->setupListener();
}

/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->mergeConfigFrom(
__DIR__ . '/../config/slow-query-logger.php', 'slow-query-logger'
);
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->mergeConfigFrom(
__DIR__ . '/../config/slow-query-logger.php', 'slow-query-logger'
);
}

/**
* setting up listener
*/
private function setupListener()
{
if (!config('slow-query-logger.enabled')) {
return;
}
/**
* setting up listener
*/
private function setupListener()
{
if (!config('slow-query-logger.enabled')) {
return;
}

DB::listen(function (QueryExecuted $queryExecuted) {
$sql = $queryExecuted->sql;
$bindings = $queryExecuted->bindings;
$time = $queryExecuted->time;
DB::listen(function (QueryExecuted $queryExecuted) {
$timeMs = $queryExecuted->time;

$logSqlQueriesSlowerThan = (float)config('slow-query-logger.time-to-log', -1);
if ($logSqlQueriesSlowerThan < 0 || $time < $logSqlQueriesSlowerThan) {
return;
}
$thresholdMs = config('slow-query-logger.threshold-ms', -1);

$level = config('slow-query-logger.log-level', 'debug');
try {
foreach ($bindings as $val) {
$sql = preg_replace('/\?/', "'{$val}'", $sql, 1);
}
if ($thresholdMs < 0 || $timeMs < $thresholdMs) {
return;
}

Log::channel('single')->log($level, $time . ' ' . $sql);
} catch (Exception $e) {
// be quiet on error
}
});
}
$sql = $queryExecuted->sql;
$bindings = $queryExecuted->bindings;

try {
if (config('slow-query-logger.replace-bindings')) {
foreach ($bindings as $bindingValue) {
$pos = strpos($sql, '?');

if ($pos !== false) {
$sql = substr_replace($sql, "'{$bindingValue}'", $pos, 1);
}
}
}

Log::channel(config('slow-query-logger.channel', 'single'))
->log(
config('slow-query-logger.level', 'debug'),
sprintf('SQL %.3f mS: %s', $timeMs, $sql),
config('slow-query-logger.show-bindings') ? ['bindings' => $bindings] : []
);

} catch (Throwable) {
// Be quiet on error.
}
});
}
}

0 comments on commit 6c02f5c

Please sign in to comment.