diff --git a/config/packages/monolog.yaml b/config/packages/monolog.yaml index 7c55740..51f8f6e 100644 --- a/config/packages/monolog.yaml +++ b/config/packages/monolog.yaml @@ -2,9 +2,22 @@ monolog: channels: ['agent'] handlers: + + # ------------------------------------------------- + # 1) Agent Channel (nur Agent Logs) + # ------------------------------------------------- agent: type: rotating_file path: '%kernel.logs_dir%/agent.log' level: debug - max_files: 14 - channels: ['agent'] \ No newline at end of file + max_files: 30 + channels: ['agent'] + + # ------------------------------------------------- + # 2) System Log (wirklich alles) + # ------------------------------------------------- + system: + type: rotating_file + path: '%kernel.logs_dir%/system.log' + level: debug + max_files: 30 \ No newline at end of file diff --git a/src/Controller/Admin/AdminSystemLogController.php b/src/Controller/Admin/AdminSystemLogController.php new file mode 100644 index 0000000..af0e56a --- /dev/null +++ b/src/Controller/Admin/AdminSystemLogController.php @@ -0,0 +1,101 @@ +logDir = \dirname(__DIR__, 3) . '/var/log'; + } + + #[Route('', name: 'admin_system_logs_index')] + public function index(): Response + { + $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN'); + + $files = []; + + foreach (\glob($this->logDir . '/system*.log') ?: [] as $file) { + $files[] = [ + 'name' => \basename($file), + 'size' => \filesize($file), + 'mtime' => \filemtime($file), + ]; + } + + \usort($files, fn($a, $b) => $b['mtime'] <=> $a['mtime']); + + return $this->render('admin/system_logs/index.html.twig', [ + 'files' => $files, + ]); + } + + #[Route( + '/view/{date}', + name: 'admin_system_logs_view', + requirements: ['date' => '\d{4}-\d{2}-\d{2}|current'] + )] + public function view(string $date): Response + { + $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN'); + + if ($date === 'current') { + $safeFilename = 'system.log'; + } else { + $safeFilename = 'system-' . $date . '.log'; + } + + $path = $this->logDir . '/' . $safeFilename; + + if (!\file_exists($path)) { + throw $this->createNotFoundException('Log file not found.'); + } + + $content = $this->readLastLines($path, 1000); + + return $this->render('admin/system_logs/view.html.twig', [ + 'filename' => $safeFilename, + 'content' => $content, + ]); + } + + private function readLastLines(string $file, int $lines = 1000): string + { + $handle = \fopen($file, 'rb'); + if (!$handle) { + return 'Unable to open log file.'; + } + + $buffer = ''; + $chunkSize = 4096; + $lineCount = 0; + $pos = -1; + + \fseek($handle, 0, SEEK_END); + $fileSize = \ftell($handle); + + while ($lineCount < $lines && -$pos < $fileSize) { + $step = \min($chunkSize, $fileSize + $pos); + \fseek($handle, $pos - $step, SEEK_END); + $chunk = \fread($handle, $step); + $buffer = $chunk . $buffer; + $lineCount += \substr_count($chunk, "\n"); + $pos -= $step; + } + + \fclose($handle); + + $linesArray = \explode("\n", $buffer); + return \implode("\n", \array_slice($linesArray, -$lines)); + } +} \ No newline at end of file diff --git a/templates/admin/base.html.twig b/templates/admin/base.html.twig index c86f044..aeac20e 100644 --- a/templates/admin/base.html.twig +++ b/templates/admin/base.html.twig @@ -127,7 +127,11 @@ - Vector-Logs Python + Vector-Log Python + + + System-Logs diff --git a/templates/admin/system_logs/index.html.twig b/templates/admin/system_logs/index.html.twig new file mode 100644 index 0000000..48cfec4 --- /dev/null +++ b/templates/admin/system_logs/index.html.twig @@ -0,0 +1,48 @@ +{% extends 'admin/base.html.twig' %} + +{% block title %}System Logs{% endblock %} + +{% block body %} +
| Datei | +Größe | +Letzte Änderung | ++ |
|---|---|---|---|
| {% set date = file.name == 'system.log' + ? 'current' + : file.name|replace({'system-':'','\.log':''}) %} + {{ file.name }} | +{{ (file.size / 1024)|number_format(2) }} KB | +{{ file.mtime|date('Y-m-d H:i:s') }} | ++ + Anzeigen + + | +
| + Keine Logdateien gefunden. + | +|||
+{{ content }}
+
+