77 lines
2.0 KiB
PHP
77 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin\System;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\File;
|
|
use Inertia\Inertia;
|
|
use Inertia\Response;
|
|
|
|
class LogController extends Controller
|
|
{
|
|
public function index(Request $request): Response
|
|
{
|
|
$logFiles = $this->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);
|
|
}
|
|
}
|