From e3348a11c8cd99baa6a5785a1a71ca779bedd753 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Thu, 18 Nov 2021 15:37:11 +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 --- kiri-engine/Abstracts/BaseObject.php | 358 ++++++++++++++------------- kiri-engine/Error/LoggerProcess.php | 18 +- 2 files changed, 195 insertions(+), 181 deletions(-) diff --git a/kiri-engine/Abstracts/BaseObject.php b/kiri-engine/Abstracts/BaseObject.php index ca27e335..03ad022e 100644 --- a/kiri-engine/Abstracts/BaseObject.php +++ b/kiri-engine/Abstracts/BaseObject.php @@ -21,199 +21,223 @@ use Swoole\Coroutine; class BaseObject implements Configure { - /** - * BaseAbstract constructor. - * - * @param array $config - * @throws Exception - */ - public function __construct(array $config = []) - { - if (!empty($config) && is_array($config)) { - Kiri::configure($this, $config); - } - } + /** + * BaseAbstract constructor. + * + * @param array $config + * @throws Exception + */ + public function __construct(array $config = []) + { + if (!empty($config) && is_array($config)) { + Kiri::configure($this, $config); + } + } - /** - * @throws Exception - */ - public function init() - { - } + /** + * @throws Exception + */ + public function init() + { + } - /** - * @param array|callable $callback - * @param object $scope - */ - public function async_create(array|callable $callback, object $scope) - { - Coroutine::create($callback, $scope); - } + /** + * @param array|callable $callback + * @param object $scope + */ + public function async_create(array|callable $callback, object $scope) + { + Coroutine::create($callback, $scope); + } - /** - * @return string - */ - #[Pure] public static function className(): string - { - return static::class; - } + /** + * @return string + */ + #[Pure] public static function className(): string + { + return static::class; + } - /** - * @param $name - * @param $value - * - * @throws Exception - */ - public function __set($name, $value) - { - $method = 'set' . ucfirst($name); - if (method_exists($this, $method)) { - $this->{$method}($value); - } else { - throw new Exception('The set name ' . $name . ' not find in class ' . static::class); - } - } + /** + * @param $name + * @param $value + * + * @throws Exception + */ + public function __set($name, $value) + { + $method = 'set' . ucfirst($name); + if (method_exists($this, $method)) { + $this->{$method}($value); + } else { + throw new Exception('The set name ' . $name . ' not find in class ' . static::class); + } + } - /** - * @param $name - * - * @return mixed - * @throws Exception - */ - public function __get($name): mixed - { - $method = 'get' . ucfirst($name); - if (method_exists($this, $method)) { - return $this->$method(); - } else { - throw new Exception('The get name ' . $name . ' not find in class ' . static::class); - } - } + /** + * @param $name + * + * @return mixed + * @throws Exception + */ + public function __get($name): mixed + { + $method = 'get' . ucfirst($name); + if (method_exists($this, $method)) { + return $this->$method(); + } else { + throw new Exception('The get name ' . $name . ' not find in class ' . static::class); + } + } - /** - * @param $message - * @param string $model - * @return bool - * @throws Exception - */ - public function addError($message, string $model = 'app'): bool - { - if ($message instanceof \Throwable) { - $this->error(jTraceEx($message)); - } else { - if (!is_string($message)) { - $message = json_encode($message, JSON_UNESCAPED_UNICODE); - } - $this->error($message); - } - return FALSE; - } + /** + * @param $message + * @param string $model + * @return bool + * @throws Exception + */ + public function addError($message, string $model = 'app'): bool + { + if ($message instanceof \Throwable) { + $this->error(jTraceEx($message)); + } else { + if (!is_string($message)) { + $message = json_encode($message, JSON_UNESCAPED_UNICODE); + } + $this->error($message); + } + return FALSE; + } - /** - * @return Logger - * @throws Exception - */ - private function logger(): Logger - { - return Kiri::getDi()->get(Logger::class); - } + /** + * @return Logger + * @throws Exception + */ + private function logger(): Logger + { + return Kiri::getDi()->get(Logger::class); + } - /** - * @param mixed $message - * @param string $method - * @param string $file - * @throws Exception - */ - public function debug(mixed $message, string $method = __METHOD__, string $file = __FILE__) - { - if (!is_string($message)) { - $message = print_r($message, true); - } - $message = "\033[35m[" . date('Y-m-d H:i:s') . '][DEBUG]: ' . $message . "\033[0m"; - - $this->logger()->debug(Logger::DEBUG, [$message, $method, $file]); - } + /** + * @param mixed $message + * @param string $method + * @param string $file + * @throws Exception + */ + public function debug(mixed $message, string $method = '', string $file = '') + { + if (!is_string($message)) { + $message = print_r($message, true); + } + $message = "\033[35m[" . date('Y-m-d H:i:s') . '][DEBUG]: ' . $message . "\033[0m"; - /** - * @param mixed $message - * @param string $method - * @param string $file - * @throws Exception - */ - public function info(mixed $message, string $method = __METHOD__, string $file = __FILE__) - { - if (!is_string($message)) { - $message = print_r($message, true); - } - $message = "\033[34m[" . date('Y-m-d H:i:s') . '][INFO]: ' . $message . "\033[0m"; + $context = []; + if (!empty($method)) $context['method'] = $method; + if (!empty($file)) $context['file'] = $file; - $this->logger()->info(Logger::NOTICE, [$message, $method, $file]); - } + $this->logger()->debug($message, $context); + } - /** - * @param mixed $message - * @param string $method - * @param string $file - * @throws Exception - */ - public function success(mixed $message, string $method = __METHOD__, string $file = __FILE__) - { - if (!is_string($message)) { - $message = print_r($message, true); - } - - $message = "\033[36m[" . date('Y-m-d H:i:s') . '][SUCCESS]: ' . $message . "\033[0m"; - - $this->logger()->notice(Logger::NOTICE, [$message, $method, $file]); - } + /** + * @param mixed $message + * @param string $method + * @param string $file + * @throws Exception + */ + public function info(mixed $message, string $method = '', string $file = '') + { + if (!is_string($message)) { + $message = print_r($message, true); + } + $message = "\033[34m[" . date('Y-m-d H:i:s') . '][INFO]: ' . $message . "\033[0m"; - /** - * @param mixed $message - * @param string $method - * @param string $file - * @throws Exception - */ - public function warning(mixed $message, string $method = __METHOD__, string $file = __FILE__) - { - if (!is_string($message)) { - $message = print_r($message, true); - } + $context = []; + if (!empty($method)) $context['method'] = $method; + if (!empty($file)) $context['file'] = $file; - $message = "\033[33m[" . date('Y-m-d H:i:s') . '][WARNING]: ' . $message . "\033[0m"; - - $this->logger()->critical(Logger::NOTICE, [$message, $method, $file]); - } + $this->logger()->info($message, $context); + } - /** - * @param mixed $message - * @param null $method - * @param null $file - * @throws Exception - */ - public function error(mixed $message, $method = null, $file = null) - { - if ($message instanceof \Throwable) { - $message = $message->getMessage() . " on line " . $message->getLine() . " at file " . $message->getFile(); - } - $content = (empty($method) ? '' : $method . ': ') . $message; + /** + * @param mixed $message + * @param string $method + * @param string $file + * @throws Exception + */ + public function success(mixed $message, string $method = '', string $file = '') + { + if (!is_string($message)) { + $message = print_r($message, true); + } - $message = "\033[41;37m[" . date('Y-m-d H:i:s') . '][ERROR]: ' . $content . "\033[0m"; + $message = "\033[36m[" . date('Y-m-d H:i:s') . '][SUCCESS]: ' . $message . "\033[0m"; - if (!empty($file)) { - $message .= PHP_EOL . "\033[41;37m[" . date('Y-m-d H:i:s') . '][ERROR]: ' . $file . "\033[0m"; - } - $this->logger()->error(Logger::ERROR, [$message, $method, $file]); - } + $context = []; + if (!empty($method)) $context['method'] = $method; + if (!empty($file)) $context['file'] = $file; + + $this->logger()->notice($message, $context); + } + + + /** + * @param mixed $message + * @param string $method + * @param string $file + * @throws Exception + */ + public function warning(mixed $message, string $method = '', string $file = '') + { + if (!is_string($message)) { + $message = print_r($message, true); + } + + $message = "\033[33m[" . date('Y-m-d H:i:s') . '][WARNING]: ' . $message . "\033[0m"; + + + $context = []; + if (!empty($method)) $context['method'] = $method; + if (!empty($file)) $context['file'] = $file; + + $this->logger()->critical($message, $context); + } + + + /** + * @param mixed $message + * @param null $method + * @param null $file + * @throws Exception + */ + public function error(mixed $message, $method = null, $file = null) + { + if ($message instanceof \Throwable) { + $message = $message->getMessage() . " on line " . $message->getLine() . " at file " . $message->getFile(); + } + $content = (empty($method) ? '' : $method . ': ') . $message; + + $message = "\033[41;37m[" . date('Y-m-d H:i:s') . '][ERROR]: ' . $content . "\033[0m"; + + if (!empty($file)) { + $message .= PHP_EOL . "\033[41;37m[" . date('Y-m-d H:i:s') . '][ERROR]: ' . $file . "\033[0m"; + } + + $context = []; + if (!empty($method)) $context['method'] = $method; + if (!empty($file)) $context['file'] = $file; + + $this->logger()->error($message, $context); + } } diff --git a/kiri-engine/Error/LoggerProcess.php b/kiri-engine/Error/LoggerProcess.php index 32c1e7dd..20dec243 100644 --- a/kiri-engine/Error/LoggerProcess.php +++ b/kiri-engine/Error/LoggerProcess.php @@ -20,22 +20,16 @@ use Server\Abstracts\BaseProcess; class LoggerProcess extends BaseProcess { - /** - * @param Process $process - * @return string - */ - #[Pure] public function getProcessName(Process $process): string - { - // TODO: Implement getProcessName() method. - return get_called_class(); - } + + public string $name = 'logger process'; + /** * @param Process $process * @throws ComponentException */ - public function onHandler(Process $process): void + public function onProcessExec(Process $process): void { // TODO: Implement onHandler() method. $this->message($process); @@ -49,10 +43,6 @@ class LoggerProcess extends BaseProcess */ public function message(Process $process) { - if ($this->checkProcessIsStop()) { - $this->exit(); - return; - } $message = Json::decode($process->read()); if (!empty($message)) { Kiri::writeFile($this->getDirName($message), $message[0], FILE_APPEND);