Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 17 additions & 2 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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()));
}
});
}

Expand Down
24 changes: 24 additions & 0 deletions src/config/query-logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the overtrue/laravel-query-logger.
*
* (c) overtrue <i@overtrue.me>
*
* 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,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thnaks, Maybe need a more appropriate name, But I don't have a better idea yet.

];