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
@@ -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);
}
}