diff --git a/config/services.yaml b/config/services.yaml index 285ac0c0..d29ee4a7 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -113,6 +113,10 @@ services: tags: - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest, priority: 101 } + App\Kobo\LogProcessor\KoboContextProcessor: + tags: + - { name: monolog.processor } + when@dev: services: Symfony\Component\HttpKernel\Profiler\Profiler: '@profiler' \ No newline at end of file diff --git a/src/Controller/Kobo/KoboDeviceController.php b/src/Controller/Kobo/KoboDeviceController.php index 15d51e15..82aa0901 100644 --- a/src/Controller/Kobo/KoboDeviceController.php +++ b/src/Controller/Kobo/KoboDeviceController.php @@ -6,10 +6,8 @@ use App\Entity\User; use App\Form\KoboType; use App\Repository\KoboDeviceRepository; -use Devdot\Monolog\Parser; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; -use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; @@ -30,35 +28,6 @@ public function index(KoboDeviceRepository $koboDeviceRepository): Response ]); } - #[Route('/logs', name: 'app_kobodevice_user_logs', methods: ['GET'])] - public function logs(ParameterBagInterface $parameterBag): Response - { - if (!$this->getUser() instanceof UserInterface) { - throw $this->createAccessDeniedException(); - } - - $records = []; - - try { - $logDir = $parameterBag->get('kernel.logs_dir'); - $env = $parameterBag->get('kernel.environment'); - - if (!is_string($logDir) || !is_string($env)) { - throw new \RuntimeException('Invalid log directory or environment'); - } - - $parser = new Parser($logDir.'/kobo.'.$env.'-'.date('Y-m-d').'.log'); - - $records = $parser->get(); - } catch (\Exception $e) { - $this->addFlash('warning', $e->getMessage()); - } - - return $this->render('kobodevice_user/logs.html.twig', [ - 'records' => $records, - ]); - } - #[Route('/new', name: 'app_kobodevice_user_new', methods: ['GET', 'POST'])] public function new(Request $request, EntityManagerInterface $entityManager): Response { diff --git a/src/Controller/Kobo/KoboDeviceLogsController.php b/src/Controller/Kobo/KoboDeviceLogsController.php new file mode 100644 index 00000000..dddeb383 --- /dev/null +++ b/src/Controller/Kobo/KoboDeviceLogsController.php @@ -0,0 +1,44 @@ +getUser() instanceof UserInterface) { + throw $this->createAccessDeniedException(); + } + + $records = []; + + try { + $parser = new Parser($this->kernelLogsDir.'/kobo.'.$this->kernelEnvironment.'-'.date('Y-m-d').'.log'); + + $records = $parser->get(); + } catch (\Exception $e) { + $this->addFlash('warning', $e->getMessage()); + } + + return $this->render('kobodevice_user/logs.html.twig', [ + 'records' => $records, + ]); + } +} diff --git a/src/Kobo/LogProcessor/KoboContextProcessor.php b/src/Kobo/LogProcessor/KoboContextProcessor.php new file mode 100644 index 00000000..2e29e3b2 --- /dev/null +++ b/src/Kobo/LogProcessor/KoboContextProcessor.php @@ -0,0 +1,47 @@ +requestStack->getCurrentRequest(); + + if (!$request instanceof Request) { + return $record; + } + + if (false === $request->attributes->has('isKoboRequest')) { + return $record; + } + + $kobo = $this->getKoboFromRequest($request); + $koboString = $kobo?->getId() ?? 'unknown'; + $record->extra['kobo'] = $koboString; + + return $record; + } + + private function getKoboFromRequest(Request $request): ?KoboDevice + { + $device = $request->attributes->get('koboDevice'); + if ($device instanceof KoboDevice) { + return $device; + } + + return $this->koboParamConverter->apply($request); + } +} diff --git a/src/Kobo/ParamConverter/KoboParamConverter.php b/src/Kobo/ParamConverter/KoboParamConverter.php index 7da71b59..706e4a10 100644 --- a/src/Kobo/ParamConverter/KoboParamConverter.php +++ b/src/Kobo/ParamConverter/KoboParamConverter.php @@ -24,6 +24,9 @@ public function supports(Request $request, ArgumentMetadata $argument): bool public function apply(Request $request): ?KoboDevice { $value = $this->getFieldValue($request); + if ($value === null) { + return null; + } return $this->bookRepository->findOneBy([$this->getFieldName() => $value]); } diff --git a/templates/kobodevice_user/logs.html.twig b/templates/kobodevice_user/logs.html.twig index 6d1f9a8d..edd7ebde 100644 --- a/templates/kobodevice_user/logs.html.twig +++ b/templates/kobodevice_user/logs.html.twig @@ -7,6 +7,8 @@ Date + Device + channel Message @@ -14,7 +16,13 @@ {% for record in records %} - {{ record.datetime|date('d.m.Y') }} {{ record.datetime|date('H:i:s') }} + {{ record.datetime|date('d.m.Y') }} {{ record.datetime|date('H:i:s') }} + + + {{ record.extra.kobo|default('unkown') }} + + + {{ record.channel }}