logger = new Logger(\config('site.id')); $this->levels = [ 'debug' => $this->logger::DEBUG, 'info' => $this->logger::INFO, 'notice' => $this->logger::NOTICE, 'warning' => $this->logger::WARNING, 'error' => $this->logger::ERROR, 'critical' => $this->logger::CRITICAL, 'alert' => $this->logger::ALERT, 'emergency' => $this->logger::EMERGENCY, ]; } /** * @param $message * @param string $model * @return bool */ public function logCategory($message, string $model = 'app'): bool { if ($message instanceof \Exception) { $this->errors[$model] = $message->getMessage(); } else { $this->errors[$model] = $message; } $this->println($message); return false; } /** * @param Throwable $exception * @param array $data * @param mixed|null $result * @return bool */ public function json_log(Throwable $exception, array $data = [], mixed $result = null): mixed { json_log($exception, $data); $this->println($exception->getMessage()); return $result; } /** * @param string $message * @return void */ public function println(string $message): void { file_put_contents('php://output', '[' . date('Y-m-d H:i:s') . '] ' . $message . PHP_EOL, FILE_APPEND); } /** * @param string $name * @param array $arguments * @return void * @throws */ public function __call(string $name, array $arguments) { try { if (method_exists($this->logger, $name)) { $this->createHandler($name)->$name(...$arguments); } else if (method_exists($this, $name)) { $this->{$name}(...$arguments); } } catch (Throwable $exception) { $this->println($exception->getMessage()); $this->json_log($exception); } } /** * @param string $name * @return Logger */ protected function createHandler(string $name): Logger { if (!$this->logger->isHandling($this->levels[$name])) { $handler = new RotatingFileHandler(APP_PATH . 'storage/logs/' . $name . '/kiri.log', $this->levels[$name]); $handler->setFormatter(new LineFormatter("[%datetime%] %channel%.%level_name%: %message% %context%\n", 'Y-m-d H:i:s')); $this->logger->pushHandler($handler); } return $this->logger; } /** * @param string $model * @return mixed */ public function getLastError(string $model = 'app'): mixed { return $this->errors[$model] ?? 'Unknown error.'; } }