diff --git a/function.php b/function.php index 60f095c1..d5361bac 100644 --- a/function.php +++ b/function.php @@ -1156,13 +1156,36 @@ if (!function_exists('error')) { * @param mixed $message * @param array $method * @return void + * @throws ReflectionException */ function error(mixed $message, array $method = []): void { + if ($message instanceof Throwable) { + $message = throwable($message); + } Kiri::getLogger()->error($message, $method); } } + +if (!function_exists('addError')) { + + /** + * @param mixed $message + * @param string $method + * @return bool + * @throws ReflectionException + */ + function addError(mixed $message, string $method = 'app'): bool + { + $logger = Kiri::getLogger(); + if ($message instanceof Throwable) { + $logger->error($message, [$message]); + } + return $logger->addError($message, $method); + } +} + if (!function_exists('success')) { /** @@ -1186,7 +1209,7 @@ if (!function_exists('throwable')) { */ function throwable(\Throwable|\Error $throwable): string { - $message = "\033[31mError: " . $throwable->getMessage() . "\033[0m" . PHP_EOL . + $message = "\033[31m" . $throwable->getMessage() . "\033[0m" . PHP_EOL . ' ' . $throwable->getFile() . " at line " . $throwable->getLine() . PHP_EOL; foreach ($throwable->getTrace() as $value) { if (!isset($value['file'])) { diff --git a/kiri-actor/Actor.php b/kiri-actor/Actor.php index a311760f..9111e63d 100644 --- a/kiri-actor/Actor.php +++ b/kiri-actor/Actor.php @@ -207,7 +207,7 @@ abstract class Actor implements ActorInterface, JsonSerializable try { $this->onUpdate(); } catch (\Throwable $exception) { - \Kiri::getLogger()->error(throwable($exception)); + error($exception); } Coroutine::sleep($this->refreshInterval / 1000); diff --git a/kiri-engine/Abstracts/Logger.php b/kiri-engine/Abstracts/Logger.php index 6f6aa9bd..0f610e50 100644 --- a/kiri-engine/Abstracts/Logger.php +++ b/kiri-engine/Abstracts/Logger.php @@ -17,7 +17,7 @@ use ReflectionException; */ class Logger implements LoggerInterface { - + const EMERGENCY = 'emergency'; const ALERT = 'alert'; const CRITICAL = 'critical'; @@ -26,11 +26,11 @@ class Logger implements LoggerInterface const NOTICE = 'notice'; const INFO = 'info'; const DEBUG = 'debug'; - - + + const LOGGER_LEVELS = [Logger::EMERGENCY, Logger::ALERT, Logger::CRITICAL, Logger::ERROR, Logger::WARNING, Logger::NOTICE, Logger::INFO, Logger::DEBUG]; - - + + /** * @param string $message * @param array $context @@ -42,8 +42,8 @@ class Logger implements LoggerInterface // TODO: Implement emergency() method. $this->log(Logger::EMERGENCY, $message, $context); } - - + + /** * @param string $message * @param array $context @@ -55,8 +55,8 @@ class Logger implements LoggerInterface // TODO: Implement alert() method. $this->log(Logger::ALERT, $message, $context); } - - + + /** * @param string $message * @param array $context @@ -68,8 +68,8 @@ class Logger implements LoggerInterface // TODO: Implement critical() method. $this->log(Logger::CRITICAL, $message, $context); } - - + + /** * @param string $message * @param array $context @@ -79,8 +79,8 @@ class Logger implements LoggerInterface // TODO: Implement error() method. $this->log(Logger::ERROR, $message, $context); } - - + + /** * @param string $message * @param array $context @@ -90,7 +90,7 @@ class Logger implements LoggerInterface // TODO: Implement warning() method. $this->log(Logger::WARNING, $message, $context); } - + /** * @param string $message * @param array $context @@ -100,8 +100,8 @@ class Logger implements LoggerInterface // TODO: Implement notice() method. $this->log(Logger::NOTICE, $message, $context); } - - + + /** * @param string $message * @param array $context @@ -111,8 +111,8 @@ class Logger implements LoggerInterface // TODO: Implement info() method. $this->log(Logger::INFO, $message, $context); } - - + + /** * @param string $message * @param array $context @@ -122,8 +122,8 @@ class Logger implements LoggerInterface // TODO: Implement debug() method. $this->log(Logger::DEBUG, $message, $context); } - - + + /** * @param mixed $level * @param string $message @@ -135,7 +135,7 @@ class Logger implements LoggerInterface // TODO: Implement log() method. $levels = Config::get('log.level', Logger::LOGGER_LEVELS); if (in_array($level, $levels)) { - $_string = '[' . now() . ']' . ucfirst($level) . ': ' . $message . PHP_EOL; + $_string = "[" . now() . ']' . ucfirst($level) . ': ' . $message . PHP_EOL; if (!empty($context)) { $_string .= $this->_string($context); } @@ -143,14 +143,14 @@ class Logger implements LoggerInterface return; } file_put_contents('php://output', $_string); - + $filename = storage('log-' . date('Y-m-d') . '.log', 'log/'); - + file_put_contents($filename, $_string, FILE_APPEND); } } - - + + /** * @return void * @throws Exception @@ -159,8 +159,8 @@ class Logger implements LoggerInterface { $this->removeFile(storage()); } - - + + /** * @param string $dirname * @return void @@ -180,8 +180,8 @@ class Logger implements LoggerInterface @unlink($path->getRealPath()); } } - - + + /** * @param $context * @return string @@ -189,11 +189,14 @@ class Logger implements LoggerInterface private function _string($context): string { if ($context instanceof \Throwable) { - $context = ['file' => $context->getFile(), 'line' => $context->getLine()]; + $context = 'file -> ' . $context->getFile() . PHP_EOL . 'line -> ' . $context->getLine() . PHP_EOL; } if (is_array($context) && isset($context[0]) && $context[0] instanceof \Throwable) { - $context = ['file' => $context[0]->getFile(), 'line' => $context[0]->getLine()]; + $context = 'file -> ' . $context[0]->getFile() . PHP_EOL . 'line -> ' . $context[0]->getLine() . PHP_EOL; } - return print_r($context, true) . PHP_EOL; + if (is_string($context)) { + return $context . PHP_EOL; + } + return implode(PHP_EOL, $context) . PHP_EOL; } } diff --git a/kiri-engine/Error/StdoutLogger.php b/kiri-engine/Error/StdoutLogger.php index 2bd9732a..1fdeba3a 100644 --- a/kiri-engine/Error/StdoutLogger.php +++ b/kiri-engine/Error/StdoutLogger.php @@ -23,7 +23,6 @@ class StdoutLogger extends Logger */ public function addError($message, string $model = 'app'): bool { - $this->error($model, [$message]); if ($message instanceof \Exception) { $this->errors[$model] = $message->getMessage(); } else { diff --git a/kiri-engine/Pool/Connection.php b/kiri-engine/Pool/Connection.php index 5c25ca11..5a1712f1 100644 --- a/kiri-engine/Pool/Connection.php +++ b/kiri-engine/Pool/Connection.php @@ -172,7 +172,7 @@ class Connection extends Component $result = true; } } catch (Error|Throwable $exception) { - $result = \Kiri::getLogger()->addError($exception, 'mysql'); + $result = addError($exception, 'mysql'); } finally { return $result; } diff --git a/kiri-engine/Redis/Redis.php b/kiri-engine/Redis/Redis.php index 4c14d501..81d70490 100644 --- a/kiri-engine/Redis/Redis.php +++ b/kiri-engine/Redis/Redis.php @@ -186,7 +186,7 @@ SCRIPT; try { $response = $client->{$name}(...$arguments); } catch (\Throwable $throwable) { - $response = \Kiri::getLogger()->addError($throwable->getMessage()); + $response = addError($throwable, 'redis'); } finally { $pool = Kiri::getDi()->get(Pool::class); $pool->push($this->host, $client);