From 93068ab22496546c0d9dc1834abfbcd958f93612 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Mon, 6 Sep 2021 11:28:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/Error/LoggerAspect.php | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/core/Error/LoggerAspect.php b/core/Error/LoggerAspect.php index 5be5e0c7..277c6190 100644 --- a/core/Error/LoggerAspect.php +++ b/core/Error/LoggerAspect.php @@ -4,6 +4,7 @@ namespace Kiri\Error; +use Exception; use Kiri\IAspect; @@ -14,8 +15,7 @@ use Kiri\IAspect; class LoggerAspect implements IAspect { - private string $className = ''; - private string $methodName = ''; + private float $time; /** @@ -25,27 +25,18 @@ class LoggerAspect implements IAspect */ public function invoke(mixed $handler, array $params = []): mixed { - $startTime = microtime(true); - - $data = call_user_func($handler, ...$params); - - $this->print_runtime($handler, $startTime); - - return $data; + return call_user_func($handler, ...$params); } /** - * @param $handler * @param $startTime + * @throws Exception */ - private function print_runtime($handler, $startTime) + private function print_runtime($startTime) { - $className = $handler[0]::class; - $methodName = $handler[1]; - $runTime = round(microtime(true) - $startTime, 6); - echo sprintf('run %s::%s use time %6f', $className, $methodName, $runTime); + echo sprintf('run %s use time %6f', request()->getUri()->__toString(), $runTime); echo PHP_EOL; } @@ -53,11 +44,16 @@ class LoggerAspect implements IAspect public function before(): void { // TODO: Implement before() method. + $this->time = microtime(true); } + /** + * @throws Exception + */ public function after(mixed $response): void { // TODO: Implement after() method. + $this->print_runtime($this->time); } }