From 49782a4f36665cb4f4a09911c6d63e93097f92f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AF=BB=E6=B0=B4=E7=9A=84=E9=B1=BC?= Date: Wed, 20 Jun 2018 12:10:18 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BC=98=E5=8C=96=20SQL=20=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=E6=97=B6=E9=97=B4=E7=9A=84=E5=B1=95=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ServiceProvider.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 5087460..696f9be 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -30,8 +30,9 @@ public function boot() $bindings = $query->connection->prepareBindings($query->bindings); $pdo = $query->connection->getPdo(); $realSql = vsprintf($sqlWithPlaceholders, array_map([$pdo, 'quote'], $bindings)); + $durationStr = $this->formatDuration($query->time / 1000); - Log::debug(sprintf('[%.4fms] %s', (\microtime(true) - LARAVEL_START) * 1000, $realSql)); + Log::debug(sprintf('[%s] %s', $durationStr, $realSql)); }); } @@ -41,4 +42,22 @@ public function boot() public function register() { } + + /** + * Format duration. + * + * @param float $seconds + * + * @return string + */ + private function formatDuration($seconds) + { + if ($seconds < 0.001) { + return round($seconds * 1000000) . 'μs'; + } elseif ($seconds < 1) { + return round($seconds * 1000, 2) . 'ms'; + } + + return round($seconds, 2) . 's'; + } } From 9734637745f73626c92b2c43a58cd4654dfba07c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AF=BB=E6=B0=B4=E7=9A=84=E9=B1=BC?= Date: Wed, 20 Jun 2018 12:26:45 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20README?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index edb7923..523e878 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@

Laravel Query Logger

-

:pencil: A dev tool to log all queries for Laravel application.

+

:pencil: A dev tool to log all queries for Laravel application.

## Installing @@ -13,14 +13,14 @@ $ composer require overtrue/laravel-query-logger --dev -vvv ## Usage ```shell -$ tail -f ./storage/logs/laravel.log +$ tail -f ./storage/logs/laravel.log ``` [2017-09-05 14:52:13] local.INFO: ============ URL: http://laravel.app/discussions =============== - [2017-09-05 14:52:14] local.INFO: [5.0083ms] select count(*) as aggregate from `discussions` where `discussions`.`deleted_at` is null - [2017-09-05 14:52:14] local.INFO: [7.0212ms] select * from `discussions` where `discussions`.`deleted_at` is null order by `is_top` desc, `created_at` desc limit 15 offset 0 - [2017-09-05 14:52:14] local.INFO: [12.4282ms] select `tags`.*, `taggables`.`taggable_id` as `pivot_taggable_id`, `taggables`.`tag_id` as `pivot_tag_id` from `tags` inner join `taggables` on `tags`.`id` = `taggables`.`tag_id` where `taggables`.`taggable_id` in ('1', '2', '3', '4', '5', '6', '7', '8') and `taggables`.`taggable_type` = 'App\\Models\\Discussion' order by `order_column` asc - [2017-09-05 14:52:14] local.INFO: [3.1729ms] select * from `users` where `users`.`id` in ('1', '2', '4') and `users`.`deleted_at` is null + [2017-09-05 14:52:14] local.DEBUG: [800μs] select count(*) as aggregate from `discussions` where `discussions`.`deleted_at` is null + [2017-09-05 14:52:14] local.DEBUG: [1.07ms] select * from `discussions` where `discussions`.`deleted_at` is null order by `is_top` desc, `created_at` desc limit 15 offset 0 + [2017-09-05 14:52:14] local.DEBUG: [3.63s] select `tags`.*, `taggables`.`taggable_id` as `pivot_taggable_id`, `taggables`.`tag_id` as `pivot_tag_id` from `tags` inner join `taggables` on `tags`.`id` = `taggables`.`tag_id` where `taggables`.`taggable_id` in ('1', '2', '3', '4', '5', '6', '7', '8') and `taggables`.`taggable_type` = 'App\\Models\\Discussion' order by `order_column` asc + [2017-09-05 14:52:14] local.DEBUG: [670μs] select * from `users` where `users`.`id` in ('1', '2', '4') and `users`.`deleted_at` is null ... ## License From 2ae7ac48c0b23c54edaf986d0182d2725d4c0292 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=89=E6=AD=A3=E8=B6=85?= Date: Wed, 20 Jun 2018 14:47:30 +0800 Subject: [PATCH 3/3] Update ServiceProvider.php --- src/ServiceProvider.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 696f9be..78f5aee 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -30,9 +30,9 @@ public function boot() $bindings = $query->connection->prepareBindings($query->bindings); $pdo = $query->connection->getPdo(); $realSql = vsprintf($sqlWithPlaceholders, array_map([$pdo, 'quote'], $bindings)); - $durationStr = $this->formatDuration($query->time / 1000); + $duration = $this->formatDuration($query->time / 1000); - Log::debug(sprintf('[%s] %s', $durationStr, $realSql)); + Log::debug(sprintf('[%s] %s', $duration, $realSql)); }); }