getLogFiles(); $selectedFile = $request->input('file'); if (! $selectedFile || ! in_array($selectedFile, $logFiles)) { $selectedFile = $logFiles[0] ?? null; } $logs = $selectedFile ? $this->parseLogFile($selectedFile) : []; return Inertia::render('admin/system/logs/index', [ 'logFiles' => $logFiles, 'selectedFile' => $selectedFile, 'logs' => $logs, ]); } private function getLogFiles(): array { $path = storage_path('logs'); if (! File::exists($path)) { return []; } $files = File::files($path); return collect($files) ->map(fn($file) => $file->getFilename()) ->filter(fn($filename) => str_ends_with($filename, '.log')) ->sortDesc() ->take(10) ->values() ->toArray(); } private function parseLogFile($filename): array { $path = storage_path("logs/{$filename}"); if (! File::exists($path)) { return []; } $content = File::get($path); $pattern = '/^\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\] (\w+)\.(\w+): ([\s\S]*?)(?=\n^\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\]|$)/m'; preg_match_all($pattern, $content, $matches, PREG_SET_ORDER); $logs = []; foreach ($matches as $index => $match) { $logs[] = [ 'id' => $index, 'timestamp' => $match[1], 'env' => $match[2], 'level' => strtoupper($match[3]), 'message' => trim($match[4]), ]; } return array_reverse($logs); } }