Feat: Add log viewer for admins

This commit is contained in:
Björn Fromme
2023-10-21 17:33:49 +02:00
parent 847af5341c
commit 2385bccb91
6 changed files with 253 additions and 0 deletions
+3
View File
@@ -14,6 +14,9 @@
<symbol id="icon-back" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="M9 15L3 9m0 0l6-6M3 9h12a6 6 0 010 12h-3" />
</symbol>
<symbol id="icon-list" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" xmlns="http://www.w3.org/2000/svg">
<path stroke-linecap="round" stroke-linejoin="round" d="M8.25 6.75h12M8.25 12h12m-12 5.25h12M3.75 6.75h.007v.008H3.75V6.75zm.375 0a.375.375 0 11-.75 0 .375.375 0 01.75 0zM3.75 12h.007v.008H3.75V12zm.375 0a.375.375 0 11-.75 0 .375.375 0 01.75 0zm-.375 5.25h.007v.008H3.75v-.008zm.375 0a.375.375 0 11-.75 0 .375.375 0 01.75 0z" />
</symbol>
<symbol id="icon-menu" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="M3.75 6.75h16.5M3.75 12h16.5m-16.5 5.25h16.5" />
</symbol>

Before

Width:  |  Height:  |  Size: 28 KiB

After

Width:  |  Height:  |  Size: 29 KiB

@@ -0,0 +1,58 @@
<?php
namespace App\Controller\Admin\Log;
use App\Entity\Log;
use App\Repository\LogRepository;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
class IndexController extends AbstractController
{
public function __construct(
private readonly LogRepository $logRepository,
private readonly PaginatorInterface $paginator
) {
}
#[Route('/admin/log', name: 'app_admin_log_index')]
#[IsGranted('ROLE_ADMIN')]
public function index(Request $request): Response
{
$qb = $this
->logRepository
->createQueryBuilder('log')
;
$pagination = $this->paginator->paginate(
$qb,
$request->query->getInt('page', 1),
10,
[
'defaultSortFieldName' => 'log.timestamp',
'defaultSortDirection' => 'DESC',
]
);
$entries = [];
foreach ($pagination as $entry) {
/** @var Log $entry */
$extra = $entry->getExtra();
$entries[] = [
'timestamp' => $entry->getTimestamp(),
'message' => $entry->getMessage(),
'user' => $extra['user'] ? $extra['user']['username'] ?? $extra['user']['email'] : '-',
'context' => json_encode($entry->getContext(), JSON_UNESCAPED_UNICODE),
];
}
return $this->render('admin/log/index.html.twig', [
'pagination' => $pagination,
'entries' => $entries,
]);
}
}
+110
View File
@@ -0,0 +1,110 @@
<?php
namespace App\Entity;
use App\Repository\LogRepository;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: LogRepository::class)]
class Log
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $message = null;
#[ORM\Column]
private ?\DateTimeImmutable $timestamp = null;
#[ORM\Column(length: 32)]
private ?string $level = null;
#[ORM\Column(length: 32)]
private ?string $channel = null;
#[ORM\Column]
private array $context = [];
#[ORM\Column]
private array $extra = [];
public function getId(): ?int
{
return $this->id;
}
public function getMessage(): ?string
{
return $this->message;
}
public function setMessage(string $message): static
{
$this->message = $message;
return $this;
}
public function getTimestamp(): ?\DateTimeImmutable
{
return $this->timestamp;
}
public function setTimestamp(\DateTimeImmutable $timestamp): static
{
$this->timestamp = $timestamp;
return $this;
}
public function getLevel(): ?string
{
return $this->level;
}
public function setLevel(string $level): static
{
$this->level = $level;
return $this;
}
public function getChannel(): ?string
{
return $this->channel;
}
public function setChannel(string $channel): static
{
$this->channel = $channel;
return $this;
}
public function getContext(): array
{
return $this->context;
}
public function setContext(array $context): static
{
$this->context = $context;
return $this;
}
public function getExtra(): array
{
return $this->extra;
}
public function setExtra(array $extra): static
{
$this->extra = $extra;
return $this;
}
}
+5
View File
@@ -101,6 +101,11 @@ class AdminMenuBuilder extends AbstractMenuBuilder
],
],
],
[
'route' => 'app_admin_log_index',
'title' => 'Logs',
'icon' => 'list',
],
];
}
+23
View File
@@ -0,0 +1,23 @@
<?php
namespace App\Repository;
use App\Entity\Log;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Log>
*
* @method Log|null find($id, $lockMode = null, $lockVersion = null)
* @method Log|null findOneBy(array $criteria, array $orderBy = null)
* @method Log[] findAll()
* @method Log[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class LogRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Log::class);
}
}
+54
View File
@@ -0,0 +1,54 @@
{% extends 'admin/layout.html.twig' %}
{% block title %}Logs{% endblock %}
{% block content %}
<h1 class="text-2xl font-bold pb-8">
Logs
</h1>
<div class="data-table-wrapper">
<div class="data-table-wrapper__inner">
<table class="data-table">
<thead>
<tr>
<th>
{{ knp_pagination_sortable(pagination, 'Datum', 'log.timestamp') }}
</th>
<th>
Benutzer
</th>
<th>
Meldung
</th>
<th></th>
</tr>
</thead>
<tbody>
{% for entry in entries %}
<tr>
<td>
<span class="whitespace-nowrap">{{ entry.timestamp|date('d.m.Y H:i') }}</span>
</td>
<td>
{{ entry.user }}
</td>
<td>
<span class="whitespace-nowrap">{{ entry.message }}</span>
</td>
<td>
{{ entry.context }}
</td>
</tr>
{% else %}
<tr>
<td colspan="4">
Keine Daten...
</td>
</tr>
{% endfor %}
</tbody>
</table>
{{ knp_pagination_render(pagination) }}
</div>
</div>
{% endblock %}