Skip to content

Commit 49782a4

Browse files
committed
优化 SQL 执行时间的展示
1 parent d72e9d9 commit 49782a4

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

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+
$durationStr = $this->formatDuration($query->time / 1000);
3334

34-
Log::debug(sprintf('[%.4fms] %s', (\microtime(true) - LARAVEL_START) * 1000, $realSql));
35+
Log::debug(sprintf('[%s] %s', $durationStr, $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)