Skip to content
Merged
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
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<h1 align="center"> Laravel Query Logger </h1>

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

## Installing

Expand All @@ -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

Expand Down
21 changes: 20 additions & 1 deletion src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
$duration = $this->formatDuration($query->time / 1000);

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

Expand All @@ -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';
}
}