diff --git a/HttpServer/Events/OnClose.php b/HttpServer/Events/OnClose.php index cf9e0e9d..546f437d 100644 --- a/HttpServer/Events/OnClose.php +++ b/HttpServer/Events/OnClose.php @@ -29,7 +29,6 @@ class OnClose extends Callback try { defer(function () { fire(Event::SYSTEM_RESOURCE_RELEASES); - logger_insert(); }); $clientInfo = $server->getClientInfo($fd); $event = Snowflake::app()->getEvent(); diff --git a/HttpServer/Events/OnConnect.php b/HttpServer/Events/OnConnect.php index 0a85da5c..46a50943 100644 --- a/HttpServer/Events/OnConnect.php +++ b/HttpServer/Events/OnConnect.php @@ -28,7 +28,6 @@ class OnConnect extends Callback { defer(function () { fire(Event::SYSTEM_RESOURCE_RELEASES); - logger_insert(); }); $event = Snowflake::app()->getEvent(); diff --git a/HttpServer/Events/OnFinish.php b/HttpServer/Events/OnFinish.php index 85e7825f..65635e08 100644 --- a/HttpServer/Events/OnFinish.php +++ b/HttpServer/Events/OnFinish.php @@ -29,7 +29,6 @@ class OnFinish extends Callback try { defer(function () { fire(Event::SYSTEM_RESOURCE_RELEASES); - logger_insert(); }); fire(Event::TASK_FINISH, [$task_id, $data]); } catch (\Throwable $exception) { diff --git a/HttpServer/Events/OnHandshake.php b/HttpServer/Events/OnHandshake.php index b1e7e876..3d3727be 100644 --- a/HttpServer/Events/OnHandshake.php +++ b/HttpServer/Events/OnHandshake.php @@ -99,7 +99,6 @@ class OnHandshake extends Callback try { defer(function () { fire(Event::SYSTEM_RESOURCE_CLEAN); - logger_insert(); }); $this->execute($request, $response); } catch (\Throwable $exception) { diff --git a/HttpServer/Events/OnMessage.php b/HttpServer/Events/OnMessage.php index 55bc822f..5ea63dbc 100644 --- a/HttpServer/Events/OnMessage.php +++ b/HttpServer/Events/OnMessage.php @@ -40,7 +40,6 @@ class OnMessage extends Callback try { defer(function () { fire(Event::SYSTEM_RESOURCE_CLEAN); - logger_insert(); }); if ($frame->opcode === 0x08) { return; diff --git a/HttpServer/Events/OnPacket.php b/HttpServer/Events/OnPacket.php index b83da590..700f780c 100644 --- a/HttpServer/Events/OnPacket.php +++ b/HttpServer/Events/OnPacket.php @@ -38,7 +38,6 @@ class OnPacket extends Callback try { defer(function () { fire(Event::SYSTEM_RESOURCE_RELEASES); - \logger_insert(); }); $request = $this->_request($clientInfo, $server, $data); diff --git a/HttpServer/Events/OnPipeMessage.php b/HttpServer/Events/OnPipeMessage.php index 91a1ed57..89fa0b5d 100644 --- a/HttpServer/Events/OnPipeMessage.php +++ b/HttpServer/Events/OnPipeMessage.php @@ -28,7 +28,6 @@ class OnPipeMessage extends Callback { defer(function () { fire(Event::SYSTEM_RESOURCE_RELEASES); - logger_insert(); }); match ($swollen_universalize['action'] ?? null) { 'kafka' => $this->onKafkaWorker($swollen_universalize), diff --git a/HttpServer/Events/OnReceive.php b/HttpServer/Events/OnReceive.php index c9b27131..91c57147 100644 --- a/HttpServer/Events/OnReceive.php +++ b/HttpServer/Events/OnReceive.php @@ -50,7 +50,6 @@ class OnReceive extends Callback try { defer(function (){ fire(Event::SYSTEM_RESOURCE_RELEASES); - \logger_insert(); }); $request = $this->_request($fd, $data, $reID); if (($node = $this->router->find_path($request)) === null) { diff --git a/HttpServer/Events/OnRequest.php b/HttpServer/Events/OnRequest.php index 712ba4e3..4917f8dd 100644 --- a/HttpServer/Events/OnRequest.php +++ b/HttpServer/Events/OnRequest.php @@ -34,7 +34,6 @@ class OnRequest extends Callback public function init() { $this->event = Snowflake::app()->getEvent(); - $this->logger = Snowflake::app()->getLogger(); } @@ -48,8 +47,7 @@ class OnRequest extends Callback { try { defer(function () { - $this->event->trigger(Event::SYSTEM_RESOURCE_RELEASES); - $this->logger->insert(); + fire(Event::SYSTEM_RESOURCE_RELEASES); }); /** @var HRequest $request */ [$request, $response] = OnRequest::createContext($request, $response); diff --git a/HttpServer/Events/OnTask.php b/HttpServer/Events/OnTask.php index dfe71485..07ef8f07 100644 --- a/HttpServer/Events/OnTask.php +++ b/HttpServer/Events/OnTask.php @@ -104,7 +104,6 @@ class OnTask extends Callback try { defer(function () { fire(Event::SYSTEM_RESOURCE_CLEAN); - logger_insert(); }); $serialize = $this->before($data); if ($serialize === null) { diff --git a/HttpServer/Events/OnWorkerError.php b/HttpServer/Events/OnWorkerError.php index ad813bc7..0ca29f48 100644 --- a/HttpServer/Events/OnWorkerError.php +++ b/HttpServer/Events/OnWorkerError.php @@ -36,8 +36,6 @@ class OnWorkerError extends Callback ); write($message, 'worker-exit'); - - logger_insert(); } } diff --git a/HttpServer/Events/OnWorkerExit.php b/HttpServer/Events/OnWorkerExit.php index 6ec86601..02ac5b1f 100644 --- a/HttpServer/Events/OnWorkerExit.php +++ b/HttpServer/Events/OnWorkerExit.php @@ -17,22 +17,21 @@ use Swoole\Timer; class OnWorkerExit extends Callback { - /** - * @param $server - * @param $worker_id - * @throws Exception - */ - public function onHandler($server, $worker_id) - { - putenv('state=exit'); - - $channel = Snowflake::app()->getChannel(); + /** + * @param $server + * @param $worker_id + * @throws Exception + */ + public function onHandler($server, $worker_id) + { + putenv('state=exit'); + $event = Snowflake::app()->getEvent(); + $channel = Snowflake::app()->getChannel(); $channel->cleanAll(); - $event = Snowflake::app()->getEvent(); - $event->trigger(Event::SERVER_WORKER_EXIT); + $event->trigger(Event::SERVER_WORKER_EXIT); - logger_insert(); + logger()->insert(); } } diff --git a/HttpServer/Events/OnWorkerStop.php b/HttpServer/Events/OnWorkerStop.php index 4ce83d64..49cc5677 100644 --- a/HttpServer/Events/OnWorkerStop.php +++ b/HttpServer/Events/OnWorkerStop.php @@ -32,9 +32,9 @@ class OnWorkerStop extends Callback $this->clearMysqlClient(); $this->clearRedisClient(); - Timer::clearAll(); + fire(Event::SYSTEM_RESOURCE_CLEAN); - logger_insert(); + Timer::clearAll(); } } diff --git a/System/Error/Logger.php b/System/Error/Logger.php index 13d6cc7f..f33cde46 100644 --- a/System/Error/Logger.php +++ b/System/Error/Logger.php @@ -14,6 +14,7 @@ use HttpServer\Http\Context; use Snowflake\Abstracts\Component; use Snowflake\Abstracts\Config; use Snowflake\Core\Json; +use Snowflake\Event; use Snowflake\Snowflake; use Swoole\Coroutine; use Swoole\Coroutine\Channel; @@ -27,266 +28,277 @@ use Throwable; class Logger extends Component { - private array $logs = []; + private array $logs = []; - /** - * @param $message - * @param string $method - * @param null $file - * @throws Exception - */ - public function debug(mixed $message, string $method = 'app', $file = null) - { - $this->writer($message, $method); - } + public function init() + { + $event = Snowflake::app()->getEvent(); + $event->on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'insert']); + $event->on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'insert']); + } - /** - * @param $message - * @param string $method - * @throws Exception - */ - public function trance($message, $method = 'app') - { - $this->writer($message, $method); - } + /** + * @param $message + * @param string $method + * @param null $file + * @throws Exception + */ + public function debug(mixed $message, string $method = 'app', $file = null) + { + $this->writer($message, $method); + } - /** - * @param $message - * @param string $method - * @param null $file - * @throws Exception - */ - public function error(mixed $message, $method = 'error', $file = null) - { - $this->writer($message, $method); - } - - /** - * @param $message - * @param string $method - * @param null $file - * @throws Exception - */ - public function success(mixed $message, $method = 'app', $file = null) - { - $this->writer($message, $method); - } - - /** - * @param $message - * @param string $method - * @return string - * @throws Exception - */ - private function writer($message, $method = 'app'): string - { - $this->print_r($message, $method); - if ($message instanceof Throwable) { - $message = $message->getMessage(); - } else { - if (is_array($message) || is_object($message)) { - $message = $this->arrayFormat($message); - } - } - if (is_array($message)) { - $message = $this->arrayFormat($message); - } - if (!empty($message)) { - $this->write($message, $method); - } - return $message; - } + /** + * @param $message + * @param string $method + * @throws Exception + */ + public function trance($message, $method = 'app') + { + $this->writer($message, $method); + } - /** - * @param $message - * @param $method - * @throws Exception - */ - public function print_r($message, $method = '') - { - $debug = Config::get('debug', ['enable' => false]); - if ((bool)$debug['enable'] === true) { - if (!is_callable($debug['callback'] ?? null, true)) { - return; - } - call_user_func($debug['callback'], $message, $method); - } - } + /** + * @param $message + * @param string $method + * @param null $file + * @throws Exception + */ + public function error(mixed $message, $method = 'error', $file = null) + { + $this->writer($message, $method); + } + + /** + * @param $message + * @param string $method + * @param null $file + * @throws Exception + */ + public function success(mixed $message, $method = 'app', $file = null) + { + $this->writer($message, $method); + } + + /** + * @param $message + * @param string $method + * @return string + * @throws Exception + */ + private function writer($message, $method = 'app'): string + { + $this->print_r($message, $method); + if ($message instanceof Throwable) { + $message = $message->getMessage(); + } else { + if (is_array($message) || is_object($message)) { + $message = $this->arrayFormat($message); + } + } + if (is_array($message)) { + $message = $this->arrayFormat($message); + } + if (!empty($message)) { + if (!is_array($this->logs)) { + $this->logs = []; + } + $this->logs[] = [$method, $message]; + } + return $message; + } - /** - * @param $message - */ - public function output($message) - { - if (str_contains($message, 'Event::rshutdown(): Event::wait()')) { - return; - } - echo $message; - } + /** + * @param $message + * @param $method + * @throws Exception + */ + public function print_r($message, $method = '') + { + $debug = Config::get('debug', ['enable' => false]); + if ((bool)$debug['enable'] === true) { + if (!is_callable($debug['callback'] ?? null, true)) { + return; + } + call_user_func($debug['callback'], $message, $method); + } + } - /** - * @param string $application - * @return mixed - */ - public function getLastError($application = 'app'): mixed - { - $filetype = []; - foreach ($this->logs as $key => $val) { - if ($val[0] != $application) { - continue; - } - $filetype[] = $val[1]; - } - if (empty($filetype)) { - return 'Unknown error.'; - } - return end($filetype); - } - - /** - * @param $messages - * @param string $method - * @throws - */ - public function write(string $messages, $method = 'app') - { - if (empty($messages)) { - return; - } - - $fileName = 'server-' . date('Y-m-d') . '.log'; - $dirName = 'log/' . (empty($method) ? 'app' : $method); - $logFile = '[' . date('Y-m-d H:i:s') . ']:' . PHP_EOL . $messages . PHP_EOL; - Snowflake::writeFile(storage($fileName, $dirName), $logFile, FILE_APPEND); - - $files = glob(storage(null, $dirName) . '/*'); - if (count($files) >= 15) { - $command = 'find ' . storage(null, $dirName) . '/ -mtime +15 -name "*.log" -exec rm -rf {} \;'; - if (Context::inCoroutine()) { - Coroutine\System::exec($command); - } else { - \shell_exec($command); - } - } - } - - /** - * @param $logFile - * @return string - */ - private function getSource($logFile): string - { - if (!file_exists($logFile)) { - Coroutine\System::exec('echo 3 > /proc/sys/vm/drop_caches'); - touch($logFile); - } - if (is_writeable($logFile)) { - $logFile = realpath($logFile); - } - return $logFile; - } + /** + * @param $message + */ + public function output($message) + { + if (str_contains($message, 'Event::rshutdown(): Event::wait()')) { + return; + } + echo $message; + } - /** - * @throws Exception - * 写入日志 - */ - public function insert() - { - if (empty($this->logs)) { - return; - } - foreach ($this->logs as $log) { - [$method, $message] = $log; - $this->write($message, $method); - } - $this->logs = []; - } + /** + * @param string $application + * @return mixed + */ + public function getLastError($application = 'app'): mixed + { + $filetype = []; + foreach ($this->logs as $key => $val) { + if ($val[0] != $application) { + continue; + } + $filetype[] = $val[1]; + } + if (empty($filetype)) { + return 'Unknown error.'; + } + return end($filetype); + } - /** - * @return array - */ - public function clear(): array - { - return $this->logs = []; - } + /** + * @param $messages + * @param string $method + * @throws + */ + public function write(string $messages, $method = 'app') + { + if (empty($messages)) { + return; + } - /** - * @param $data - * @return string - */ - private function arrayFormat($data): string - { - if (is_string($data)) { - return $data; - } - if ($data instanceof Throwable) { - $data = $this->getException($data); - } else if (is_object($data)) { - $data = get_object_vars($data); - } + $fileName = 'server-' . date('Y-m-d') . '.log'; + $dirName = 'log/' . (empty($method) ? 'app' : $method); + $logFile = '[' . date('Y-m-d H:i:s') . ']:' . PHP_EOL . $messages . PHP_EOL; + Snowflake::writeFile(storage($fileName, $dirName), $logFile, FILE_APPEND); - $filetype = []; - foreach ($data as $key => $val) { - if (is_array($val)) { - $filetype[] = $this->arrayFormat($val); - } else { - $filetype[] = (is_string($key) ? $key . ' : ' : '') . $val; - } - } - return implode(PHP_EOL, $filetype); - } + $files = glob(storage(null, $dirName) . '/*'); + if (count($files) >= 15) { + $command = 'find ' . storage(null, $dirName) . '/ -mtime +15 -name "*.log" -exec rm -rf {} \;'; + if (Context::inCoroutine()) { + Coroutine\System::exec($command); + } else { + \shell_exec($command); + } + } + } + + /** + * @param $logFile + * @return string + */ + private function getSource($logFile): string + { + if (!file_exists($logFile)) { + Coroutine\System::exec('echo 3 > /proc/sys/vm/drop_caches'); + touch($logFile); + } + if (is_writeable($logFile)) { + $logFile = realpath($logFile); + } + return $logFile; + } - /** - * @param Throwable $exception - * @return mixed - * @throws Exception - */ - public function exception(Throwable $exception): mixed - { - $errorInfo = [ - 'message' => $exception->getMessage(), - 'file' => $exception->getFile(), - 'line' => $exception->getLine() - ]; - $this->error(var_export($errorInfo, true)); + /** + * @throws Exception + * 写入日志 + */ + public function insert() + { + if (empty($this->logs)) { + return; + } + foreach ($this->logs as $log) { + [$method, $message] = $log; + $this->write($message, $method); + } + $this->logs = []; + } - $code = $exception->getCode() == 0 ? 500 : $exception->getCode(); + /** + * @return array + */ + public function clear(): array + { + return $this->logs = []; + } - $logger = Snowflake::app()->getLogger(); + /** + * @param $data + * @return string + */ + private function arrayFormat($data): string + { + if (is_string($data)) { + return $data; + } + if ($data instanceof Throwable) { + $data = $this->getException($data); + } else if (is_object($data)) { + $data = get_object_vars($data); + } - $string = 'Exception: ' . PHP_EOL; - $string .= '#. message: ' . $errorInfo['message'] . PHP_EOL; - $string .= '#. file: ' . $errorInfo['file'] . PHP_EOL; - $string .= '#. line: ' . $errorInfo['line'] . PHP_EOL; - - $logger->write($string . $exception->getTraceAsString(), 'trace'); - $logger->write(jTraceEx($exception), 'exception'); - - return Json::to($code, $errorInfo['message'], [ - 'file' => $exception->getFile(), - 'line' => $exception->getLine() - ]); - } + $filetype = []; + foreach ($data as $key => $val) { + if (is_array($val)) { + $filetype[] = $this->arrayFormat($val); + } else { + $filetype[] = (is_string($key) ? $key . ' : ' : '') . $val; + } + } + return implode(PHP_EOL, $filetype); + } - /** - * @param Throwable $exception - * @return array - */ - private function getException(Throwable $exception): array - { - $filetype = [$exception->getMessage()]; - $filetype[] = $exception->getFile() . ' on line ' . $exception->getLine(); - $filetype[] = $exception->getTrace(); - return $filetype; - } + /** + * @param Throwable $exception + * @return mixed + * @throws Exception + */ + public function exception(Throwable $exception): mixed + { + $errorInfo = [ + 'message' => $exception->getMessage(), + 'file' => $exception->getFile(), + 'line' => $exception->getLine() + ]; + $this->error(var_export($errorInfo, true)); + + $code = $exception->getCode() == 0 ? 500 : $exception->getCode(); + + $logger = Snowflake::app()->getLogger(); + + $string = 'Exception: ' . PHP_EOL; + $string .= '#. message: ' . $errorInfo['message'] . PHP_EOL; + $string .= '#. file: ' . $errorInfo['file'] . PHP_EOL; + $string .= '#. line: ' . $errorInfo['line'] . PHP_EOL; + + $logger->write($string . $exception->getTraceAsString(), 'trace'); + $logger->write(jTraceEx($exception), 'exception'); + + return Json::to($code, $errorInfo['message'], [ + 'file' => $exception->getFile(), + 'line' => $exception->getLine() + ]); + } + + + /** + * @param Throwable $exception + * @return array + */ + private function getException(Throwable $exception): array + { + $filetype = [$exception->getMessage()]; + $filetype[] = $exception->getFile() . ' on line ' . $exception->getLine(); + $filetype[] = $exception->getTrace(); + return $filetype; + } } diff --git a/function.php b/function.php index b7a6c629..c7b9fdc5 100644 --- a/function.php +++ b/function.php @@ -426,18 +426,6 @@ if (!function_exists('logger')) { } } -if (!function_exists('logger_insert')) { - - - /** - * @throws Exception - */ - function logger_insert(): void - { - Snowflake::app()->getLogger()->insert(); - } -} - if (!function_exists('get_file_extension')) { function get_file_extension($filename)