diff --git a/core/Abstracts/Logger.php b/core/Abstracts/Logger.php index 7a94ddf7..fdce1f9b 100644 --- a/core/Abstracts/Logger.php +++ b/core/Abstracts/Logger.php @@ -17,184 +17,184 @@ use Server\Events\OnWorkerStop; class Logger implements LoggerInterface { - const EMERGENCY = 'emergency'; - const ALERT = 'alert'; - const CRITICAL = 'critical'; - const ERROR = 'error'; - const WARNING = 'warning'; - const NOTICE = 'notice'; - const INFO = 'info'; - const DEBUG = 'debug'; + const EMERGENCY = 'emergency'; + const ALERT = 'alert'; + const CRITICAL = 'critical'; + const ERROR = 'error'; + const WARNING = 'warning'; + const NOTICE = 'notice'; + const INFO = 'info'; + const DEBUG = 'debug'; - /** - * @var EventProvider - */ - #[Inject(EventProvider::class)] - public EventProvider $eventProvider; + /** + * @var EventProvider + */ + #[Inject(EventProvider::class)] + public EventProvider $eventProvider; - private array $_loggers = []; + private array $_loggers = []; - const LOGGER_LEVELS = [Logger::EMERGENCY, Logger::ALERT, Logger::CRITICAL, Logger::ERROR, Logger::WARNING, Logger::NOTICE, Logger::INFO, Logger::DEBUG]; + const LOGGER_LEVELS = [Logger::EMERGENCY, Logger::ALERT, Logger::CRITICAL, Logger::ERROR, Logger::WARNING, Logger::NOTICE, Logger::INFO, Logger::DEBUG]; - /** - * 监听事件 - */ - public function init() - { - $this->eventProvider->on(OnAfterRequest::class, [$this, 'onAfterRequest']); - $this->eventProvider->on(OnWorkerStop::class, [$this, 'onAfterRequest']); - } + /** + * 监听事件 + */ + public function init() + { + $this->eventProvider->on(OnAfterRequest::class, [$this, 'onAfterRequest']); + $this->eventProvider->on(OnWorkerStop::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 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 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 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 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 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 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 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 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 - */ - public 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; - } + /** + * @param mixed $level + * @param string $message + * @param array $context + * @throws ConfigException + */ + public 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); + $_string = '[' . now() . '] production.' . $level . ': ' . $this->_string($message, $context); - echo $_string; + file_put_contents('php://output', $_string); - $this->_loggers[] = $_string; - } + $this->_loggers[] = $_string; + } - /** - * @param OnAfterRequest|OnWorkerStop $param - * @throws Exception - */ - public function onAfterRequest(OnAfterRequest|OnWorkerStop $param) - { - $loggers = implode(PHP_EOL, $this->_loggers); - $this->_loggers = []; - if (!empty($loggers)) { - $filename = storage('log-' . date('Y-m-d') . '.log', 'logs/'); + /** + * @param OnAfterRequest|OnWorkerStop $param + * @throws Exception + */ + public function onAfterRequest(OnAfterRequest|OnWorkerStop $param) + { + $loggers = implode(PHP_EOL, $this->_loggers); + $this->_loggers = []; + if (!empty($loggers)) { + $filename = storage('log-' . date('Y-m-d') . '.log', 'logs/'); - file_put_contents($filename, $loggers); - } - } + 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; - } + /** + * @param $message + * @param $context + * @return string + */ + private function _string($message, $context): string + { + return $message . ' ' . print_r($context, true) . PHP_EOL; + } }