From 1ef28546b9be680a2d62914934f5875f7e179d88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Tue, 24 Aug 2021 19:11:14 +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/Abstracts/BaseObject.php | 1 - core/Abstracts/Logger.php | 195 +++++++++++++++++++++++++++++++ core/Application.php | 2 +- core/Di/Container.php | 7 +- core/Error/Logger.php | 189 +++--------------------------- function.php | 20 +++- http-helper/Controller.php | 11 ++ http-server/Abstracts/Http.php | 1 - http-server/Abstracts/Server.php | 10 ++ 9 files changed, 253 insertions(+), 183 deletions(-) create mode 100644 core/Abstracts/Logger.php diff --git a/core/Abstracts/BaseObject.php b/core/Abstracts/BaseObject.php index f3af8f91..52d357d7 100644 --- a/core/Abstracts/BaseObject.php +++ b/core/Abstracts/BaseObject.php @@ -74,7 +74,6 @@ class BaseObject implements Configure if (method_exists($this, $method)) { $this->{$method}($value); } else { - $this->error('set ' . $name . ' not exists ' . static::class); throw new Exception('The set name ' . $name . ' not find in class ' . static::class); } } diff --git a/core/Abstracts/Logger.php b/core/Abstracts/Logger.php new file mode 100644 index 00000000..d1c0486e --- /dev/null +++ b/core/Abstracts/Logger.php @@ -0,0 +1,195 @@ +eventProvider->on(OnAfterRequest::class, [$this, 'onAfterRequest']); + } + + + /** + * @param string $message + * @param array $context + * @throws ConfigException + * + * 紧急情况 + */ + public function emergency($message, array $context = array()) + { + // TODO: Implement emergency() method. + $this->log(Logger::EMERGENCY, $message, $context); + } + + + /** + * @param string $message + * @param array $context + * @throws ConfigException + * + * 应该警惕的 + */ + public function alert($message, array $context = array()) + { + // TODO: Implement alert() method. + $this->log(Logger::ALERT, $message, $context); + } + + + /** + * @param string $message + * @param array $context + * @throws ConfigException + * + * 关键性的日志 + */ + public function critical($message, array $context = array()) + { + // TODO: Implement critical() method. + $this->log(Logger::CRITICAL, $message, $context); + } + + + /** + * @param string $message + * @param array $context + * @throws ConfigException + */ + public function error($message, array $context = array()) + { + // TODO: Implement error() method. + $this->log(Logger::ERROR, $message, $context); + } + + + /** + * @param string $message + * @param array $context + * @throws ConfigException + */ + public function warning($message, array $context = array()) + { + // TODO: Implement warning() method. + $this->log(Logger::WARNING, $message, $context); + } + + /** + * @param string $message + * @param array $context + * @throws ConfigException + */ + public function notice($message, array $context = array()) + { + // TODO: Implement notice() method. + $this->log(Logger::NOTICE, $message, $context); + } + + + /** + * @param string $message + * @param array $context + * @throws ConfigException + */ + public function info($message, array $context = array()) + { + // TODO: Implement info() method. + $this->log(Logger::INFO, $message, $context); + } + + + /** + * @param string $message + * @param array $context + * @throws ConfigException + */ + public function debug($message, array $context = array()) + { + // TODO: Implement debug() method. + $this->log(Logger::DEBUG, $message, $context); + } + + + /** + * @param mixed $level + * @param string $message + * @param array $context + * @throws ConfigException + */ + private function log($level, $message, array $context = array()) + { + // TODO: Implement log() method. + $levels = Config::get('log.level', Logger::LOGGER_LEVELS); + if (!in_array($level, $levels)) { + return; + } + + $_string = '[' . now() . '] production.' . $level . ': ' . $this->_string($message, $context); + + $this->_loggers[] = $_string; + } + + + /** + * @param OnAfterRequest $afterRequest + * @throws Exception + */ + public function onAfterRequest(OnAfterRequest $afterRequest) + { + $loggers = implode(PHP_EOL, $this->_loggers); + $this->_loggers = []; + + $filename = storage('log-' . date('Y-m-d') . '.log', 'logs/'); + + file_put_contents($filename, $loggers); + } + + + /** + * @param $message + * @param $context + * @return string + */ + private function _string($message, $context): string + { + return $message . ' ' . print_r($context, true) . PHP_EOL; + } +} diff --git a/core/Application.php b/core/Application.php index 479e04f1..4a399704 100644 --- a/core/Application.php +++ b/core/Application.php @@ -213,7 +213,7 @@ class Application extends BaseApplication $data = $this->getBuilder($manager->exec($class)); } catch (\Throwable $exception) { - $data = $this->getBuilder(logger()->exception($exception)); + $data = $this->getBuilder(jTraceEx($exception)); } finally { print_r($data); Timer::clearAll(); diff --git a/core/Di/Container.php b/core/Di/Container.php index 91cdfa03..fda6473a 100644 --- a/core/Di/Container.php +++ b/core/Di/Container.php @@ -11,10 +11,12 @@ namespace Kiri\Di; use Annotation\Inject; use Exception; -use Http\Server; use Kiri\Abstracts\BaseObject; +use Kiri\Abstracts\Logger; use Kiri\Exception\NotFindClassException; use Kiri\Kiri; +use Psr\Log\LoggerInterface; +use Psr\Log\Test\TestLogger; use ReflectionClass; use ReflectionException; use ReflectionFunction; @@ -63,7 +65,8 @@ class Container extends BaseObject implements ContainerInterface /** @var array|string[] */ private array $_interfaces = [ RequestInterface::class => Request::class, - ResponseInterface::class => Response::class + ResponseInterface::class => Response::class, + LoggerInterface::class => Logger::class ]; diff --git a/core/Error/Logger.php b/core/Error/Logger.php index c992ee72..01b6f849 100644 --- a/core/Error/Logger.php +++ b/core/Error/Logger.php @@ -17,13 +17,14 @@ use Kiri\Core\Json; use Kiri\Events\EventProvider; use Kiri\Exception\ConfigException; use Kiri\Kiri; -use Server\Events\OnAfterRequest; +use Psr\Log\LoggerInterface; use Swoole\Coroutine; use Throwable; /** * Class Logger * @package Kiri\Kiri\Error + * @mixin \Kiri\Abstracts\Logger */ class Logger extends Component { @@ -36,136 +37,25 @@ class Logger extends Component public EventProvider $eventProvider; + /** + * inject logger + * + * @var LoggerInterface + */ + #[Inject(LoggerInterface::class)] + public LoggerInterface $logger; + + private array $sources = []; - /** - * - */ - public function init() - { - $this->eventProvider->on(OnAfterRequest::class, [$this, 'insert']); - } - - - /** - * @param mixed $message - * @param string $method - * @param null $file - * @throws Exception - */ - public function debug(mixed $message, string $method = 'app', $file = null) - { - if (Config::get('environment', 'pro') == 'pro') { - return; - } - $this->output($message); - } - - - /** - * @param mixed $message - * @param string $method - * @throws Exception - */ - public function trance(mixed $message, string $method = 'app') - { - if (Config::get('environment', 'pro') == 'pro') { - return; - } - $this->output($message); - } - - - /** - * @param mixed $message - * @param string $method - * @param null $file - * @throws Exception - */ - public function error(mixed $message, $method = 'error', $file = null) - { - $this->writer($message, $method); - } - - /** - * @param mixed $message - * @param string $method - * @param null $file - * @throws Exception - */ - public function success(mixed $message, string $method = 'app', $file = null) - { - if (Config::get('environment', 'pro') == 'pro') { - return; - } - $this->output($message); - } - - /** - * @param $message - * @param string $method - * @return void - * @throws Exception - */ - private function writer($message, string $method = 'app'): void - { - if (empty($message)) { - return; - } - $message = print_r($message, true); - $this->print_r($message, $method); - if (!is_array($this->logs)) { - $this->logs = []; - } - $this->logs[$method] = $message; - } - - - /** - * @param $message - * @param string $method - * @throws Exception - */ - public function print_r($message, string $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 - * @throws ConfigException - */ - public function output($message, string $method = 'default') - { - if (Config::get('environment', 'dev') == 'pro') { - if ($method === 'error') { - echo $message; - } - return; - } - if (str_contains($message, 'Event::rshutdown(): Event::wait()')) { - return; - } - echo $message; - } - - /** * @param string $application - * @return mixed + * @return string */ - public function getLastError(string $application = 'app'): mixed + public function getLastError(string $application = 'app'): string { - return $this->logs[$application] ?? 'Unknown error.'; + return 'Unknown error.'; } /** @@ -187,21 +77,6 @@ class Logger extends Component } - /** - * @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; - } /** @@ -219,31 +94,6 @@ class Logger extends Component $this->logs = []; } - /** - * @return array - */ - public function clear(): array - { - return $this->logs = []; - } - - /** - * @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); - } - return Json::encode($data); - } - /** * @param Throwable $exception @@ -264,16 +114,5 @@ class Logger extends Component } - /** - * @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 a38b0da6..53c2cf3d 100644 --- a/function.php +++ b/function.php @@ -10,8 +10,8 @@ use Http\Context\Response as HttpResponse; use Http\Route\Router; use JetBrains\PhpStorm\Pure; use Kiri\Abstracts\Config; -use Kiri\AspectManager; use Kiri\Application; +use Kiri\AspectManager; use Kiri\Core\ArrayAccess; use Kiri\Error\Logger; use Kiri\Events\EventDispatch; @@ -19,6 +19,7 @@ use Kiri\Events\EventProvider; use Kiri\Exception\ConfigException; use Kiri\Exception\NotFindClassException; use Kiri\Kiri; +use Psr\Log\LoggerInterface; use Server\Constrict\Request; use Server\Constrict\Response; use Swoole\WebSocket\Server; @@ -54,6 +55,19 @@ if (!function_exists('make')) { } + +if (!function_exists('now')) { + + /** + * @return string + */ + function now(): string + { + return date('Y-m-d H:i:s') . '.' . str_replace('0.', '', (string)microtime(true)); + } +} + + if (!function_exists('workerName')) { @@ -751,7 +765,7 @@ if (!function_exists('event')) { function event($name, $callback, bool $isAppend = true) { $pro = di(EventProvider::class); - $pro->on($name, $callback,0); + $pro->on($name, $callback, 0); } } @@ -1119,7 +1133,7 @@ if (!function_exists('error')) { */ function error(mixed $message, string $method = 'error') { - Kiri::app()->error($message, $method); + Kiri::getDi()->get(LoggerInterface::class)->error($method, [$message]); } } diff --git a/http-helper/Controller.php b/http-helper/Controller.php index 25a66895..69233dd6 100644 --- a/http-helper/Controller.php +++ b/http-helper/Controller.php @@ -8,6 +8,7 @@ use Annotation\Inject; use JetBrains\PhpStorm\Pure; use Kiri\Application; use Kiri\Di\ContainerInterface; +use Psr\Log\LoggerInterface; use Server\RequestInterface; use Server\ResponseInterface; @@ -55,4 +56,14 @@ class Controller + /** + * inject logger + * + * @var LoggerInterface + */ + #[Inject(LoggerInterface::class)] + public LoggerInterface $logger; + + + } diff --git a/http-server/Abstracts/Http.php b/http-server/Abstracts/Http.php index 8274d1f5..35c4ce57 100644 --- a/http-server/Abstracts/Http.php +++ b/http-server/Abstracts/Http.php @@ -40,7 +40,6 @@ abstract class Http extends Server implements OnRequest /** * @throws ReflectionException * @throws ConfigException - * @throws NotFindClassException */ public function init() { diff --git a/http-server/Abstracts/Server.php b/http-server/Abstracts/Server.php index c582bb41..b04a48be 100644 --- a/http-server/Abstracts/Server.php +++ b/http-server/Abstracts/Server.php @@ -4,10 +4,13 @@ namespace Server\Abstracts; +use Annotation\Inject; use Exception; use Kiri\Abstracts\Config; +use Kiri\Abstracts\Logger; use Kiri\Exception\ConfigException; use Kiri\Kiri; +use Psr\Log\LoggerInterface; /** @@ -18,6 +21,13 @@ abstract class Server { + /** + * @var LoggerInterface + */ + #[Inject(LoggerInterface::class)] + public LoggerInterface $logger; + + /** * @param $prefix * @throws ConfigException