logger = new Logger(\config('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 failure($message, string $model = 'app'): bool { if ($message instanceof \Exception) { $this->errors[$model] = $message->getMessage(); } else { $this->errors[$model] = $message; } return $this->dump($message); } /** * @param $message * @return bool */ protected function dump($message): bool { $message = throwable($message); if (str_contains($message, 'inotify_rm_watch')) { return false; } file_put_contents('php://output', '[' . date('Y-m-d H:i:s') . '] ' . $message, FILE_APPEND); $this->error($message, []); return false; } /** * @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) { file_put_contents('php://output', '[' . date('Y-m-d H:i:s') . '] ' . $exception->getMessage(), FILE_APPEND); } } /** * @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.'; } }