vendor/symfony/monolog-bridge/Processor/DebugProcessor.php line 71

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Bridge\Monolog\Processor;
  11. use Monolog\Logger;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
  15. use Symfony\Contracts\Service\ResetInterface;
  16. class DebugProcessor implements DebugLoggerInterfaceResetInterface
  17. {
  18.     private $records = [];
  19.     private $errorCount = [];
  20.     private $requestStack;
  21.     public function __construct(RequestStack $requestStack null)
  22.     {
  23.         $this->requestStack $requestStack;
  24.     }
  25.     public function __invoke(array $record)
  26.     {
  27.         $hash $this->requestStack && ($request $this->requestStack->getCurrentRequest()) ? spl_object_hash($request) : '';
  28.         $timestamp $timestampRfc3339 false;
  29.         if ($record['datetime'] instanceof \DateTimeInterface) {
  30.             $timestamp $record['datetime']->getTimestamp();
  31.             $timestampRfc3339 $record['datetime']->format(\DateTimeInterface::RFC3339_EXTENDED);
  32.         } elseif (false !== $timestamp strtotime($record['datetime'])) {
  33.             $timestampRfc3339 = (new \DateTimeImmutable($record['datetime']))->format(\DateTimeInterface::RFC3339_EXTENDED);
  34.         }
  35.         $this->records[$hash][] = [
  36.             'timestamp' => $timestamp,
  37.             'timestamp_rfc3339' => $timestampRfc3339,
  38.             'message' => $record['message'],
  39.             'priority' => $record['level'],
  40.             'priorityName' => $record['level_name'],
  41.             'context' => $record['context'],
  42.             'channel' => $record['channel'] ?? '',
  43.         ];
  44.         if (!isset($this->errorCount[$hash])) {
  45.             $this->errorCount[$hash] = 0;
  46.         }
  47.         switch ($record['level']) {
  48.             case Logger::ERROR:
  49.             case Logger::CRITICAL:
  50.             case Logger::ALERT:
  51.             case Logger::EMERGENCY:
  52.                 ++$this->errorCount[$hash];
  53.         }
  54.         return $record;
  55.     }
  56.     /**
  57.      * {@inheritdoc}
  58.      */
  59.     public function getLogs(Request $request null)
  60.     {
  61.         if (null !== $request) {
  62.             return $this->records[spl_object_hash($request)] ?? [];
  63.         }
  64.         if (=== \count($this->records)) {
  65.             return [];
  66.         }
  67.         return array_merge(...array_values($this->records));
  68.     }
  69.     /**
  70.      * {@inheritdoc}
  71.      */
  72.     public function countErrors(Request $request null)
  73.     {
  74.         if (null !== $request) {
  75.             return $this->errorCount[spl_object_hash($request)] ?? 0;
  76.         }
  77.         return array_sum($this->errorCount);
  78.     }
  79.     /**
  80.      * {@inheritdoc}
  81.      */
  82.     public function clear()
  83.     {
  84.         $this->records = [];
  85.         $this->errorCount = [];
  86.     }
  87.     /**
  88.      * {@inheritdoc}
  89.      */
  90.     public function reset()
  91.     {
  92.         $this->clear();
  93.     }
  94. }