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,
]);
}
}