src/Logger/LogProcessor.php line 87

  1. <?php
  2. namespace App\Logger;
  3. use App\Entity\User;
  4. use Monolog\LogRecord;
  5. use Monolog\Level;
  6. use Symfony\Component\Asset\Context\RequestStackContext;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. use Symfony\Component\HttpKernel\Event\RequestEvent;
  9. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  10. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  11. class LogProcessor
  12. {
  13.     /**
  14.      * @var User
  15.      */
  16.     private $user;
  17.     private $pathInfo;
  18.     private $uri;
  19.     private $method;
  20.     private $params;
  21.     private $json;
  22.     private $tokenStorage;
  23.     public function __construct(TokenStorageInterface $tokenStorage)
  24.     {
  25.         $this->tokenStorage $tokenStorage;
  26.     }
  27.     // this method is called for each log record; optimize it to not hurt performance
  28.     public function __invoke(LogRecord $record): LogRecord
  29.     {
  30.         // Skip processing 404 errors to prevent them from being logged/emailed
  31.         if ($this->is404Error($record)) {
  32.             // Create a new record with DEBUG level instead of directly modifying it
  33.             return $record->with(levelLevel::Debug);
  34.         }
  35.         
  36.         $record $record->with(extraarray_merge($record->extra, [
  37.             'url' => $this->pathInfo,
  38.             'uri' => $this->uri,
  39.             'method' => $this->method,
  40.             'params' => $this->params,
  41.             'json' => $this->json,
  42.             'user' => 'anon.'
  43.         ]));
  44.         if (!$this->user) {
  45.             return $record;
  46.         }
  47.         $email $this->user->getEmail();
  48.         
  49.         return $record->with(extraarray_merge($record->extra, [
  50.             'user' => $email
  51.         ]));
  52.     }
  53.     /**
  54.      * Check if the log record is for a 404 error
  55.      */
  56.     private function is404Error(LogRecord $record): bool
  57.     {
  58.         // Check if this is a NotFoundHttpException
  59.         if (isset($record['context']['exception']) && $record['context']['exception'] instanceof NotFoundHttpException) {
  60.             return true;
  61.         }
  62.         
  63.         // Check message content for 404 error patterns
  64.         if (isset($record['message']) && (
  65.             strpos($record['message'], 'NotFoundHttpException') !== false ||
  66.             strpos($record['message'], 'No route found') !== false
  67.         )) {
  68.             return true;
  69.         }
  70.         
  71.         return false;
  72.     }
  73.     public function onKernelRequest(RequestEvent $event): void
  74.     {
  75.         $request $event->getRequest();
  76.         $this->pathInfo $request->getPathInfo();
  77.         $this->method $request->getMethod();
  78.         $this->uri $request->getRequestUri();
  79.     
  80.         if ('GET' === $this->method) {
  81.             $params $request->query->all();
  82.         } elseif ('application/json' === $request->getContentTypeFormat()) {
  83.             $content $request->getContent();
  84.             $params json_decode($contenttrue) ?? [];
  85.             $this->json $content;
  86.         } else {
  87.             $params $request->request->all();
  88.         }
  89.     
  90.         $this->params json_encode($params);
  91.     
  92.         $token $this->tokenStorage->getToken();
  93.         if (null === $token) {
  94.             return;
  95.         }
  96.     
  97.         $user $token->getUser();
  98.         if (!is_object($user)) { // e.g. anonymous authentication
  99.             return;
  100.         }
  101.         
  102.         $this->user $user;    
  103.     }
  104. }