diff --git a/src/log/composer.json b/src/log/composer.json index 43f566e33..2c8a81f17 100644 --- a/src/log/composer.json +++ b/src/log/composer.json @@ -20,8 +20,6 @@ "swoft/stdlib": "~2.0.0" }, "autoload": { - "classmap": [ - ], "psr-4": { "Swoft\\Log\\": "src/" } diff --git a/src/log/phpunit.xml b/src/log/phpunit.xml index 15311104a..e491bd4a9 100644 --- a/src/log/phpunit.xml +++ b/src/log/phpunit.xml @@ -1,14 +1,14 @@ + bootstrap="test/bootstrap.php" + backupGlobals="false" + backupStaticAttributes="false" + colors="true" + convertErrorsToExceptions="true" + convertNoticesToExceptions="true" + convertWarningsToExceptions="true" + processIsolation="false" + stopOnFailure="false"> ./test/unit @@ -19,4 +19,4 @@ ./src/* - \ No newline at end of file + diff --git a/src/log/src/AutoLoader.php b/src/log/src/AutoLoader.php index ebfe60527..08aa3013c 100644 --- a/src/log/src/AutoLoader.php +++ b/src/log/src/AutoLoader.php @@ -2,9 +2,9 @@ namespace Swoft\Log; -use function dirname; use Swoft\Helper\ComposerJSON; use Swoft\SwoftComponent; +use function dirname; /** * Class AutoLoader diff --git a/src/log/src/CLogger.php b/src/log/src/CLogger.php index d156692cf..d9ee05522 100644 --- a/src/log/src/CLogger.php +++ b/src/log/src/CLogger.php @@ -1,11 +1,10 @@ 'INFO', self::DEBUG => 'DEBUG', self::WARNING => 'WARNING', self::ERROR => 'ERROR', - ); + ]; /** * Logger constructor. @@ -63,7 +62,7 @@ public function __construct() * * @return bool */ - public function addRecord($level, $message, array $context = array()): bool + public function addRecord($level, $message, array $context = []): bool { if (!$this->enable) { return true; diff --git a/src/log/src/Handler/CEchoHandler.php b/src/log/src/Handler/CEchoHandler.php index 160e0f681..1e3af9415 100644 --- a/src/log/src/Handler/CEchoHandler.php +++ b/src/log/src/Handler/CEchoHandler.php @@ -1,17 +1,16 @@ levelValues)) { return true; @@ -128,4 +127,4 @@ public function isHandling(array $record) return in_array($record['level'], $this->levelValues, true); } -} \ No newline at end of file +} diff --git a/src/log/src/Handler/CFileHandler.php b/src/log/src/Handler/CFileHandler.php index b939509f9..928fea5a4 100644 --- a/src/log/src/Handler/CFileHandler.php +++ b/src/log/src/Handler/CFileHandler.php @@ -1,12 +1,16 @@ logFile[0] === '@') { + if (strpos($this->logFile, '@') === 0) { $this->init(); $this->bootingRecords[] = $record; @@ -57,17 +61,14 @@ protected function write(array $record): void } else { $records = array_column($records, 'formatted'); } - $messageText = implode("\n", $records) . "\n"; + $message = implode("\n", $records) . "\n"; $logFile = $this->formatFile($this->logFile); // Not all console log in coroutine - $res = file_put_contents($logFile, $messageText, FILE_APPEND); - - if ($res === false) { - throw new InvalidArgumentException( - sprintf('Unable to append to log file: %s', $logFile) - ); + $count = file_put_contents($logFile, $message, FILE_APPEND); + if ($count === false) { + throw new InvalidArgumentException(sprintf('Unable to append to log file: %s', $logFile)); } } @@ -76,7 +77,8 @@ protected function write(array $record): void */ public function setLevels(string $levels): void { - $levelNames = explode(',', $levels); + $levelNames = explode(',', $levels); + $this->levelValues = Logger::getLevelByNames($levelNames); $this->levels = $levels; diff --git a/src/log/src/Handler/FileHandler.php b/src/log/src/Handler/FileHandler.php index 93583eaf3..727632e59 100644 --- a/src/log/src/Handler/FileHandler.php +++ b/src/log/src/Handler/FileHandler.php @@ -1,17 +1,15 @@ format('Y-m-d H:i:s'); } + return JsonHelper::encode($record, JSON_UNESCAPED_UNICODE); } @@ -169,9 +163,8 @@ private function createDir(): void if ($logDir !== null && !is_dir($logDir)) { $status = mkdir($logDir, 0777, true); if ($status === false) { - throw new UnexpectedValueException( - sprintf('There is no existing directory at "%s" and its not buildable: ', $logDir) - ); + $errMsg = sprintf('There is no existing directory at "%s" and its not buildable', $logDir); + throw new UnexpectedValueException($errMsg); } } } diff --git a/src/log/src/Helper/CLog.php b/src/log/src/Helper/CLog.php index 40fcf424c..71c404649 100644 --- a/src/log/src/Helper/CLog.php +++ b/src/log/src/Helper/CLog.php @@ -1,12 +1,12 @@ debug($message, []); + self::$cLogger->debug($message, $context); } } @@ -83,11 +89,17 @@ public static function debug(string $message, ...$params): void */ public static function info(string $message, ...$params): void { + $context = []; + if ($params) { - $message = sprintf($message, ...$params); + if (is_array($params[0])) { + $context = $params[0]; + } else { + $message = sprintf($message, ...$params); + } } - self::$cLogger->info($message, []); + self::$cLogger->info($message, $context); } /** @@ -98,11 +110,17 @@ public static function info(string $message, ...$params): void */ public static function warning(string $message, ...$params): void { + $context = []; + if ($params) { - $message = sprintf($message, ...$params); + if (is_array($params[0])) { + $context = $params[0]; + } else { + $message = sprintf($message, ...$params); + } } - self::$cLogger->warning($message, []); + self::$cLogger->warning($message, $context); } /** @@ -113,10 +131,16 @@ public static function warning(string $message, ...$params): void */ public static function error(string $message, ...$params): void { + $context = []; + if ($params) { - $message = sprintf($message, ...$params); + if (is_array($params[0])) { + $context = $params[0]; + } else { + $message = sprintf($message, ...$params); + } } - self::$cLogger->error($message, []); + self::$cLogger->error($message, $context); } } diff --git a/src/log/src/Helper/Log.php b/src/log/src/Helper/Log.php index f2904e903..0774cb0a3 100644 --- a/src/log/src/Helper/Log.php +++ b/src/log/src/Helper/Log.php @@ -1,9 +1,7 @@ emergency($message, $context); } @@ -30,7 +29,8 @@ public static function emergency(string $message, ...$params): bool */ public static function debug(string $message, ...$params): bool { - [$message, $context] = self::formatLog($message, ...$params); + [$message, $context] = self::formatLog($message, $params); + if (APP_DEBUG) { return self::getLogger()->debug($message, $context); } @@ -46,7 +46,8 @@ public static function debug(string $message, ...$params): bool */ public static function alert(string $message, ...$params): bool { - [$message, $context] = self::formatLog($message, ...$params); + [$message, $context] = self::formatLog($message, $params); + return self::getLogger()->alert($message, $context); } @@ -58,7 +59,8 @@ public static function alert(string $message, ...$params): bool */ public static function info(string $message, ...$params): bool { - [$message, $context] = self::formatLog($message, ...$params); + [$message, $context] = self::formatLog($message, $params); + return self::getLogger()->info($message, $context); } @@ -70,7 +72,8 @@ public static function info(string $message, ...$params): bool */ public static function warning(string $message, ...$params): bool { - [$message, $context] = self::formatLog($message, ...$params); + [$message, $context] = self::formatLog($message, $params); + return self::getLogger()->warning($message, $context); } @@ -82,7 +85,8 @@ public static function warning(string $message, ...$params): bool */ public static function error(string $message, ...$params): bool { - [$message, $context] = self::formatLog($message, ...$params); + [$message, $context] = self::formatLog($message, $params); + return self::getLogger()->error($message, $context); } @@ -107,7 +111,7 @@ public static function pushLog(string $key, $val): void */ public static function profileStart(string $name, ...$params): void { - if (!empty($params)) { + if ($params) { $name = sprintf($name, ...$params); } @@ -134,7 +138,7 @@ public static function counting(string $name, int $hit, int $total = null): void */ public static function profileEnd(string $name, ...$params): void { - if (!empty($params)) { + if ($params) { $name = sprintf($name, ...$params); } @@ -155,18 +159,18 @@ public static function getLogger(): Logger * * @return array */ - public static function formatLog(string $message, ...$params): array + public static function formatLog(string $message, array $params): array { - $firstParam = $params[0] ?? null; - if (is_array($firstParam)) { - return [$message, $firstParam]; - } - - if (!empty($params)) { - $message = sprintf($message, ...$params); - return [$message, []]; + $context = []; + + if ($params) { + if (is_array($params[0])) { + $context = $params[0]; + } else { + $message = sprintf($message, ...$params); + } } - return [$message, []]; + return [$message, $context]; } } diff --git a/src/log/src/Logger.php b/src/log/src/Logger.php index bd367f10f..24d67c820 100644 --- a/src/log/src/Logger.php +++ b/src/log/src/Logger.php @@ -1,16 +1,17 @@ messages[] = $record; - if (count($this->messages) >= $this->flushInterval) { $this->flushLog(); } @@ -208,8 +205,6 @@ public function addRecord($level, $message, array $context = []): bool * @param array $extra * * @return array - * @throws SwoftException - * @throws SwoftException */ public function formatRecord( string $message, @@ -219,7 +214,6 @@ public function formatRecord( DateTime $ts, array $extra ): array { - $record = [ 'messages' => $message, 'context' => $context, @@ -238,7 +232,6 @@ public function formatRecord( $record[$item] = context()->get($item, ''); } - return $record; } @@ -323,6 +316,7 @@ public function getProfilesInfos(): string continue; } $cost = sprintf('%.2f', $profile['cost'] * 1000); + $profileAry[] = "$key=" . $cost . '(ms)/' . $profile['total']; } @@ -338,7 +332,11 @@ public function getProfilesInfos(): string */ public function counting(string $name, int $hit, int $total = null): void { - if (!is_string($name) || empty($name)) { + if (!$this->enable) { + return; + } + + if (!$name) { return; } @@ -441,7 +439,6 @@ public function flushLog(): void $this->messages = []; reset($this->handlers); - while ($handler = current($this->handlers)) { $handler->handleBatch($messages); next($this->handlers); @@ -460,6 +457,7 @@ public function appendNoticeLog($flush = false): void if (!$this->enable) { return; } + $cid = Co::tid(); $ts = $this->getLoggerTime(); @@ -494,9 +492,6 @@ public function appendNoticeLog($flush = false): void * Format notice message * * @return array - * @throws SwoftException - * @throws SwoftException - * @throws SwoftException */ private function formatNoticeMessage(): array { @@ -514,7 +509,7 @@ private function formatNoticeMessage(): array if ($this->json) { $messageAry = [ - 'cost(ms)' => (float)$timeUsed, + 'cost(ms)' => (float)$timeUsed, 'mem(MB)' => (float)$memUsed, 'uri' => $this->getUri(), 'pushLog' => implode(' ', $pushLogs), @@ -651,7 +646,6 @@ public function getItems(): array * Request uri * * @return string - * @throws SwoftException */ private function getUri(): string { @@ -662,7 +656,6 @@ private function getUri(): string * Request time * * @return float - * @throws SwoftException */ private function getRequestTime(): float { diff --git a/src/log/test/testing/bean.php b/src/log/test/testing/bean.php index 1a6c808f4..98bd2f54f 100644 --- a/src/log/test/testing/bean.php +++ b/src/log/test/testing/bean.php @@ -5,7 +5,7 @@ use Swoft\Log\Logger; return [ - 'lineFormatter' => [ + 'lineFormatter' => [ 'class' => LineFormatter::class, 'format' => '%datetime% [%level_name%] [%channel%] [%event%] [tid:%tid%] [cid:%cid%] [traceid:%traceid%] [spanid:%spanid%] [parentid:%parentid%] %messages%', 'dateFormat' => 'Y-m-d H:i:s', @@ -16,7 +16,7 @@ 'formatter' => bean('lineFormatter'), 'levels' => 'error,warning', ], - 'logger' => [ + 'logger' => [ 'class' => Logger::class, 'flushRequest' => false, 'enable' => false, @@ -25,4 +25,4 @@ 'notice' => bean('noticeHandler'), ], ] -]; \ No newline at end of file +]; diff --git a/src/log/test/unit/FileHandlerTest.php b/src/log/test/unit/FileHandlerTest.php index ee1302f57..1ddbc9539 100644 --- a/src/log/test/unit/FileHandlerTest.php +++ b/src/log/test/unit/FileHandlerTest.php @@ -1,9 +1,7 @@ getHandler()->formatFile("notice-%d{Y-m-d}.log"); + $result = $this->getHandler()->formatFile('notice-%d{Y-m-d}.log'); $this->assertEquals($result, 'notice-' . date('Y-m-d') . '.log'); - $result = $this->getHandler()->formatFile("notice.log"); + $result = $this->getHandler()->formatFile('notice.log'); $this->assertEquals($result, 'notice.log'); - $result = $this->getHandler()->formatFile("%d{Y-m-d}notice.log"); + $result = $this->getHandler()->formatFile('%d{Y-m-d}notice.log'); $this->assertEquals($result, date('Y-m-d') . 'notice.log'); - $result = $this->getHandler()->formatFile("notice.log%d{Y-m-d}"); + $result = $this->getHandler()->formatFile('notice.log%d{Y-m-d}'); $this->assertEquals($result, 'notice.log' . date('Y-m-d')); } @@ -34,4 +32,4 @@ private function getHandler(): FileHandler { return BeanFactory::getBean('testFileHandler'); } -} \ No newline at end of file +} diff --git a/src/log/test/unit/LogTest.php b/src/log/test/unit/LogTest.php index e8a67fa41..6534de413 100644 --- a/src/log/test/unit/LogTest.php +++ b/src/log/test/unit/LogTest.php @@ -1,9 +1,7 @@ assertEquals($result, ['messageabc', []]); + $result = Log::formatLog('message %s%s%s', ['a', 'b', 'c']); + $this->assertEquals($result, ['message abc', []]); - $result = Log::formatLog('message%s'); + $result = Log::formatLog('message%s', []); $this->assertEquals($result, ['message%s', []]); $result = Log::formatLog('message%s', ['a' => 'b']); $this->assertEquals($result, ['message%s', ['a' => 'b']]); } -} \ No newline at end of file +}