diff --git a/README.md b/README.md index 641aedb..c13eda0 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,12 @@ Laravel Query Logger will be enabled when `APP_DEBUG` is `true`. > Please keep the `--dev` option. +Publish configuration file (optional) to select the size in seconds minimum to log. + +``` +php artisan vendor:publish --provider="Overtrue\LaravelQueryLogger\ServiceProvider" +``` + ## Usage ```shell diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 7931e3f..d39e446 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -23,6 +23,16 @@ class ServiceProvider extends LaravelServiceProvider */ public function boot() { + // Config files + $this->publishes([realpath(__DIR__).'/config/query-logger.php' => config_path('query-logger.php')], 'config'); + $this->mergeConfigFrom(realpath(__DIR__).'/config/query-logger.php', 'query-logger'); + + // Disable if config is false + if (false === config('query-logger.enabled', true)) { + return; + } + + // Start code if (!$this->app['config']->get('app.debug')) { return; } @@ -33,9 +43,14 @@ public function boot() $bindings = $query->connection->prepareBindings($query->bindings); $pdo = $query->connection->getPdo(); $realSql = vsprintf($sqlWithPlaceholders, array_map([$pdo, 'quote'], $bindings)); - $duration = $this->formatDuration($query->time / 1000); + $max_miliseconds = config('query-logger.miliseconds', 0); + $seconds = $query->time / 1000; + $duration = $this->formatDuration($seconds); + $miliseconds = $seconds * 1000; - Log::debug(sprintf('[%s] %s | %s: %s', $duration, $realSql, request()->method(), request()->getRequestUri())); + if ($miliseconds >= $max_miliseconds) { + Log::debug(sprintf('[%s] %s | %s: %s', $duration, $realSql, request()->method(), request()->getRequestUri())); + } }); } diff --git a/src/config/query-logger.php b/src/config/query-logger.php new file mode 100644 index 0000000..2876de8 --- /dev/null +++ b/src/config/query-logger.php @@ -0,0 +1,24 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +return [ + /* + * Enable or disable + */ + + 'enabled' => env('QUERY_LOGGER_ENABLED', true), + + /* + * Min size of query to log in miliseconds + */ + + 'miliseconds' => 0, +];