Skip to content

Commit

Permalink
Expanded logging to all routes
Browse files Browse the repository at this point in the history
  • Loading branch information
LauraWebdev committed Dec 27, 2023
1 parent 8e840f0 commit 55e8dbd
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions src/Listener/ApiLogListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,32 @@ public function __construct(EntityManagerInterface $em, Security $security)
}

/**
* Creates a database log entry for API routes
* Creates a database log entry for routes
*
* @param RequestEvent $event The event object containing the request information.
* @return void
*/
public function onKernelRequest(RequestEvent $event): void
{
$request = $event->getRequest();

if (str_starts_with($request->getPathInfo(), '/api') && !str_starts_with($request->getPathInfo(), '/api/docs')) {
$user = $this->security->getUser();

try {
$newLog = new ApiLog();
$newLog->setIp($request->getClientIp());
$newLog->setTimestamp(new \DateTime());
$newLog->setUserAgent($request->headers->get('User-Agent'));
$newLog->setEndpoint($request->getPathInfo());

if (null !== $user) {
$username = $user->getUsername();
$user = $this->em->getRepository(User::class)->findOneBy(['username' => $username]);
$newLog->setUser($user);
}

$this->em->persist($newLog);
$this->em->flush();
} catch (\Exception $e) {}
$user = $this->security->getUser();

try {
$newLog = new ApiLog();
$newLog->setIp($request->getClientIp());
$newLog->setTimestamp(new \DateTime());
$newLog->setUserAgent($request->headers->get('User-Agent'));
$newLog->setEndpoint($request->getPathInfo());

if (null !== $user) {
$username = $user->getUsername();
$user = $this->em->getRepository(User::class)->findOneBy(['username' => $username]);
$newLog->setUser($user);
}

$this->em->persist($newLog);
$this->em->flush();
} catch (\Exception $e) {
}
}

Expand Down

0 comments on commit 55e8dbd

Please sign in to comment.