Skip to content

Commit 6c02f5c

Browse files
committed
Upgrade to all laravel versions to 9.x
A few tweaks made to suit our needs. May move the threashold to micro-seconds in a later release.
1 parent dac7b69 commit 6c02f5c

File tree

4 files changed

+127
-105
lines changed

4 files changed

+127
-105
lines changed

README.md

+11-10
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
composer require rokde/laravel-slow-query-logger
99
```
1010

11-
Look into your `laravel.log` file to see your messy queries.
11+
Look into your `laravel.log` file to see your slow queries.
1212

1313
## Installation
1414

1515
Add to your composer.json following lines
1616

17-
"require": {
18-
"rokde/laravel-slow-query-logger": "^1.*"
19-
}
17+
"require": {
18+
"rokde/laravel-slow-query-logger": "^1.*"
19+
}
2020

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

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

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

37-
### `log-level`
37+
### `level`
3838

3939
Set the log-level for logging the slow queries.
4040

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

43-
### `time-to-log`
43+
### `threshold-ms`
4444

45-
Only log queries longer than this value in microseconds.
45+
Only log queries longer than this value in milliseconds.
4646

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

4950
## Usage
5051

51-
Start your application and look into your logs to identify the slow queries.
52+
Start your application and look into your logs to identify the slow queries.

composer.json

+26-27
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
{
2-
"name": "rokde/laravel-slow-query-logger",
3-
"description": "Slow Query Logger for Laravel",
4-
"license": "MIT",
5-
"authors": [
6-
{
7-
"name": "Robert Kummer",
8-
"email": "[email protected]"
9-
}
10-
],
11-
"autoload": {
12-
"psr-4": {
13-
"Rokde\\LaravelSlowQueryLogger\\": "src/"
14-
}
15-
},
16-
"minimum-stability": "stable",
17-
"require": {
18-
"php": "^7.1.3",
19-
"illuminate/database": "^5.6",
20-
"illuminate/support": "^5.6"
21-
},
22-
"extra": {
23-
"laravel": {
24-
"providers": [
25-
"Rokde\\LaravelSlowQueryLogger\\LaravelSlowQueryLoggerProvider"
26-
]
27-
}
28-
}
2+
"name": "rokde/laravel-slow-query-logger",
3+
"description": "Slow Query Logger for Laravel",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "Robert Kummer",
8+
"email": "[email protected]"
9+
}
10+
],
11+
"autoload": {
12+
"psr-4": {
13+
"Rokde\\LaravelSlowQueryLogger\\": "src/"
14+
}
15+
},
16+
"minimum-stability": "stable",
17+
"require": {
18+
"illuminate/database": "^5.6|^7.0|^8.0|^9.0",
19+
"illuminate/support": "^5.6|^7.0|^8.0|^9.0"
20+
},
21+
"extra": {
22+
"laravel": {
23+
"providers": [
24+
"Rokde\\LaravelSlowQueryLogger\\LaravelSlowQueryLoggerProvider"
25+
]
26+
}
27+
}
2928
}

config/slow-query-logger.php

+26-17
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
11
<?php
22

33
return [
4-
/**
5-
* log when you are on these environments
6-
*/
7-
'enabled' => env('LARAVEL_SLOW_QUERY_LOGGER_ENABLED', false),
4+
/**
5+
* log when you are on these environments
6+
*/
7+
'enabled' => env('LARAVEL_SLOW_QUERY_LOGGER_ENABLED', false),
88

9-
/**
10-
* log when you are on these environments
11-
*/
12-
'channel' => env('LARAVEL_SLOW_QUERY_LOGGER_CHANNEL', 'single'),
9+
/**
10+
* log when you are on these environments
11+
*/
12+
'channel' => env('LARAVEL_SLOW_QUERY_LOGGER_CHANNEL', 'single'),
1313

14-
/**
15-
* level to log
16-
*/
17-
'log-level' => env('LARAVEL_SLOW_QUERY_LOGGER_LOG_LEVEL', 'debug'),
14+
/**
15+
* Log level to use.
16+
*/
17+
'level' => env('LARAVEL_SLOW_QUERY_LOGGER_LEVEL', 'debug'),
1818

19-
/**
20-
* log all sql queries that are slower than X seconds
21-
* laravel measures at a precision of 2 digits, so 0.7134 will be logged as 0.71
22-
*/
23-
'time-to-log' => env('LARAVEL_SLOW_QUERY_LOGGER_TIME_TO_LOG', 0.7),
19+
/**
20+
* Log the bindings separately in the context.
21+
*/
22+
'show-bindings' => env('LARAVEL_SLOW_QUERY_LOGGER_SHOW_BINDINGS', false),
23+
24+
/**
25+
* Substitute the bindings in the placeholders of the query.
26+
*/
27+
'replace-bindings' => env('LARAVEL_SLOW_QUERY_LOGGER_REPLACE_BINDINGS', false),
28+
29+
/**
30+
* log all sql queries that are slower than a threashold milli-seconds
31+
*/
32+
'threshold-ms' => env('LARAVEL_SLOW_QUERY_LOGGER_THRESHOLD_MS', 700),
2433
];

src/LaravelSlowQueryLoggerProvider.php

+64-51
Original file line numberDiff line numberDiff line change
@@ -2,69 +2,82 @@
22

33
namespace Rokde\LaravelSlowQueryLogger;
44

5-
use Exception;
65
use Illuminate\Database\Events\QueryExecuted;
76
use Illuminate\Support\Facades\DB;
87
use Illuminate\Support\Facades\Log;
98
use Illuminate\Support\ServiceProvider;
9+
use Throwable;
1010

1111
class LaravelSlowQueryLoggerProvider extends ServiceProvider
1212
{
13-
/**
14-
* Bootstrap the application services.
15-
*/
16-
public function boot()
17-
{
18-
if ($this->app->runningInConsole()) {
19-
$this->publishes([
20-
__DIR__ . '/../config/slow-query-logger.php' => config_path('slow-query-logger.php'),
21-
], 'config');
22-
}
13+
/**
14+
* Bootstrap the application services.
15+
*/
16+
public function boot()
17+
{
18+
if ($this->app->runningInConsole()) {
19+
$this->publishes([
20+
__DIR__ . '/../config/slow-query-logger.php' => config_path('slow-query-logger.php'),
21+
], 'config');
22+
}
2323

24-
$this->setupListener();
25-
}
24+
$this->setupListener();
25+
}
2626

27-
/**
28-
* Register the application services.
29-
*
30-
* @return void
31-
*/
32-
public function register()
33-
{
34-
$this->mergeConfigFrom(
35-
__DIR__ . '/../config/slow-query-logger.php', 'slow-query-logger'
36-
);
37-
}
27+
/**
28+
* Register the application services.
29+
*
30+
* @return void
31+
*/
32+
public function register()
33+
{
34+
$this->mergeConfigFrom(
35+
__DIR__ . '/../config/slow-query-logger.php', 'slow-query-logger'
36+
);
37+
}
3838

39-
/**
40-
* setting up listener
41-
*/
42-
private function setupListener()
43-
{
44-
if (!config('slow-query-logger.enabled')) {
45-
return;
46-
}
39+
/**
40+
* setting up listener
41+
*/
42+
private function setupListener()
43+
{
44+
if (!config('slow-query-logger.enabled')) {
45+
return;
46+
}
4747

48-
DB::listen(function (QueryExecuted $queryExecuted) {
49-
$sql = $queryExecuted->sql;
50-
$bindings = $queryExecuted->bindings;
51-
$time = $queryExecuted->time;
48+
DB::listen(function (QueryExecuted $queryExecuted) {
49+
$timeMs = $queryExecuted->time;
5250

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

58-
$level = config('slow-query-logger.log-level', 'debug');
59-
try {
60-
foreach ($bindings as $val) {
61-
$sql = preg_replace('/\?/', "'{$val}'", $sql, 1);
62-
}
53+
if ($thresholdMs < 0 || $timeMs < $thresholdMs) {
54+
return;
55+
}
6356

64-
Log::channel('single')->log($level, $time . ' ' . $sql);
65-
} catch (Exception $e) {
66-
// be quiet on error
67-
}
68-
});
69-
}
57+
$sql = $queryExecuted->sql;
58+
$bindings = $queryExecuted->bindings;
59+
60+
try {
61+
if (config('slow-query-logger.replace-bindings')) {
62+
foreach ($bindings as $bindingValue) {
63+
$pos = strpos($sql, '?');
64+
65+
if ($pos !== false) {
66+
$sql = substr_replace($sql, "'{$bindingValue}'", $pos, 1);
67+
}
68+
}
69+
}
70+
71+
Log::channel(config('slow-query-logger.channel', 'single'))
72+
->log(
73+
config('slow-query-logger.level', 'debug'),
74+
sprintf('SQL %.3f mS: %s', $timeMs, $sql),
75+
config('slow-query-logger.show-bindings') ? ['bindings' => $bindings] : []
76+
);
77+
78+
} catch (Throwable) {
79+
// Be quiet on error.
80+
}
81+
});
82+
}
7083
}

0 commit comments

Comments
 (0)