Skip to content

Commit 3d9f54f

Browse files
authored
Merge pull request #2 from ruchengtang/clean-code
优化 SQL 执行时间的展示
2 parents d72e9d9 + 2ae7ac4 commit 3d9f54f

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<h1 align="center"> Laravel Query Logger </h1>
22

3-
<p align="center"> :pencil: A dev tool to log all queries for Laravel application.</p>
3+
<p align="center"> :pencil: A dev tool to log all queries for Laravel application.</p>
44

55
## Installing
66

@@ -13,14 +13,14 @@ $ composer require overtrue/laravel-query-logger --dev -vvv
1313
## Usage
1414

1515
```shell
16-
$ tail -f ./storage/logs/laravel.log
16+
$ tail -f ./storage/logs/laravel.log
1717
```
1818

1919
[2017-09-05 14:52:13] local.INFO: ============ URL: http://laravel.app/discussions ===============
20-
[2017-09-05 14:52:14] local.INFO: [5.0083ms] select count(*) as aggregate from `discussions` where `discussions`.`deleted_at` is null
21-
[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
22-
[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
23-
[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
20+
[2017-09-05 14:52:14] local.DEBUG: [800μs] select count(*) as aggregate from `discussions` where `discussions`.`deleted_at` is null
21+
[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
22+
[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
23+
[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
2424
...
2525
## License
2626

src/ServiceProvider.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ public function boot()
3030
$bindings = $query->connection->prepareBindings($query->bindings);
3131
$pdo = $query->connection->getPdo();
3232
$realSql = vsprintf($sqlWithPlaceholders, array_map([$pdo, 'quote'], $bindings));
33+
$duration = $this->formatDuration($query->time / 1000);
3334

34-
Log::debug(sprintf('[%.4fms] %s', (\microtime(true) - LARAVEL_START) * 1000, $realSql));
35+
Log::debug(sprintf('[%s] %s', $duration, $realSql));
3536
});
3637
}
3738

@@ -41,4 +42,22 @@ public function boot()
4142
public function register()
4243
{
4344
}
45+
46+
/**
47+
* Format duration.
48+
*
49+
* @param float $seconds
50+
*
51+
* @return string
52+
*/
53+
private function formatDuration($seconds)
54+
{
55+
if ($seconds < 0.001) {
56+
return round($seconds * 1000000) . 'μs';
57+
} elseif ($seconds < 1) {
58+
return round($seconds * 1000, 2) . 'ms';
59+
}
60+
61+
return round($seconds, 2) . 's';
62+
}
4463
}

0 commit comments

Comments
 (0)