This repository has been archived by the owner on Feb 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
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
Robert Kummer
committed
Jun 28, 2016
0 parents
commit 892b63f
Showing
4 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
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,45 @@ | ||
# Slow Query Logger for Laravel | ||
|
||
[![Latest Stable Version](https://poser.pugx.org/rokde/laravel-slow-query-logger/v/stable.svg)](https://packagist.org/packages/rokde/laravel-slow-query-logger) [![Latest Unstable Version](https://poser.pugx.org/rokde/laravel-slow-query-logger/v/unstable.svg)](https://packagist.org/packages/rokde/laravel-slow-query-logger) [![License](https://poser.pugx.org/rokde/laravel-slow-query-logger/license.svg)](https://packagist.org/packages/rokde/laravel-slow-query-logger) [![Total Downloads](https://poser.pugx.org/rokde/laravel-slow-query-logger/downloads.svg)](https://packagist.org/packages/rokde/laravel-slow-query-logger) | ||
|
||
## Quickstart | ||
|
||
``` | ||
composer require rokde/laravel-slow-query-logger | ||
``` | ||
|
||
Add to `providers` in `config/app.php`: | ||
|
||
``` | ||
Rokde\LaravelSlowQueryLogger\LaravelSlowQueryLoggerProvider::class, | ||
``` | ||
|
||
## Installation | ||
|
||
Add to your composer.json following lines | ||
|
||
"require": { | ||
"rokde/laravel-slow-query-logger": "~0.0" | ||
} | ||
|
||
Add `Rokde\LaravelSlowQueryLogger\LaravelSlowQueryLoggerProvider::class,` to `providers` in `config/app.php`. | ||
|
||
Run `php artisan vendor:publish --provider="Rokde\LaravelSlowQueryLogger\LaravelSlowQueryLoggerProvider"` | ||
|
||
## Configuration | ||
|
||
### `time-to-log` | ||
|
||
Only log queries longer than this value in microseconds. | ||
|
||
### `environments` | ||
|
||
Set the enabled environments to log slow queries. | ||
|
||
### `log-level` | ||
|
||
Set the log-level for logging the slow queries. | ||
|
||
## Usage | ||
|
||
Nothing to do after adding to `/config/app.php`. Watch your logs. |
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,22 @@ | ||
{ | ||
"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": ">=5.5.9", | ||
"illuminate/support": "5.2.*", | ||
"illuminate/database": "5.2.*" | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
|
||
return [ | ||
/** | ||
* 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' => 0.7, | ||
|
||
/** | ||
* log when you are on these environments | ||
*/ | ||
'environments' => [ | ||
'local', | ||
], | ||
|
||
/** | ||
* level to log | ||
*/ | ||
'log-level' => 'debug', | ||
]; |
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,74 @@ | ||
<?php | ||
|
||
namespace Rokde\LaravelSlowQueryLogger; | ||
|
||
use Illuminate\Contracts\Events\Dispatcher; | ||
use Illuminate\Database\Events\QueryExecuted; | ||
use Illuminate\Log\Writer; | ||
use Illuminate\Support\ServiceProvider; | ||
|
||
class LaravelSlowQueryLoggerProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Bootstrap the application services. | ||
* @param Dispatcher $events | ||
* @param Writer $log | ||
*/ | ||
public function boot(Dispatcher $events, Writer $log) | ||
{ | ||
$this->publishes([ | ||
__DIR__ . '/../config/slow-query-logger.php' => config_path('slow-query-logger.php'), | ||
], 'config'); | ||
|
||
$this->setupListener($events, $log); | ||
} | ||
|
||
/** | ||
* Register the application services. | ||
* | ||
* @return void | ||
*/ | ||
public function register() | ||
{ | ||
$this->mergeConfigFrom( | ||
__DIR__ . '/../config/slow-query-logger.php', 'slow-query-logger' | ||
); | ||
} | ||
|
||
/** | ||
* setting up listener | ||
* | ||
* @param Dispatcher $events | ||
* @param Writer $log | ||
*/ | ||
private function setupListener(Dispatcher $events, Writer $log) | ||
{ | ||
$environments = config('slow-query-logger.environments', []); | ||
|
||
if (!$this->app->environment($environments)) { | ||
return; | ||
} | ||
|
||
$events->listen(QueryExecuted::class, function (QueryExecuted $queryExecuted) use ($log) { | ||
$sql = $queryExecuted->sql; | ||
$bindings = $queryExecuted->bindings; | ||
$time = $queryExecuted->time; | ||
|
||
$logSqlQueriesSlowerThan = config('slow-query-logger.time-to-log'); | ||
if ($logSqlQueriesSlowerThan < 0 || $time < $logSqlQueriesSlowerThan) { | ||
return; | ||
} | ||
|
||
$level = config('slow-query-logger.log-level', 'debug'); | ||
try { | ||
foreach ($bindings as $val) { | ||
$sql = preg_replace('/\?/', "'{$val}'", $sql, 1); | ||
} | ||
|
||
$log->log($level, $time . ' ' . $sql); | ||
} catch (\Exception $e) { | ||
// be quiet on error | ||
} | ||
}); | ||
} | ||
} |