vendor/symfony/http-kernel/DataCollector/LoggerDataCollector.php line 67

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\Component\HttpKernel\DataCollector;
  11. use Symfony\Component\Debug\Exception\SilencedErrorContext;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
  16. /**
  17.  * LogDataCollector.
  18.  *
  19.  * @author Fabien Potencier <fabien@symfony.com>
  20.  */
  21. class LoggerDataCollector extends DataCollector implements LateDataCollectorInterface
  22. {
  23.     private $logger;
  24.     private $containerPathPrefix;
  25.     private $currentRequest;
  26.     private $requestStack;
  27.     public function __construct($logger nullstring $containerPathPrefix nullRequestStack $requestStack null)
  28.     {
  29.         if (null !== $logger && $logger instanceof DebugLoggerInterface) {
  30.             $this->logger $logger;
  31.         }
  32.         $this->containerPathPrefix $containerPathPrefix;
  33.         $this->requestStack $requestStack;
  34.     }
  35.     /**
  36.      * {@inheritdoc}
  37.      */
  38.     public function collect(Request $requestResponse $response, \Exception $exception null)
  39.     {
  40.         $this->currentRequest $this->requestStack && $this->requestStack->getMasterRequest() !== $request $request null;
  41.     }
  42.     /**
  43.      * {@inheritdoc}
  44.      */
  45.     public function reset()
  46.     {
  47.         if ($this->logger instanceof DebugLoggerInterface) {
  48.             $this->logger->clear();
  49.         }
  50.         $this->data = [];
  51.     }
  52.     /**
  53.      * {@inheritdoc}
  54.      */
  55.     public function lateCollect()
  56.     {
  57.         if (null !== $this->logger) {
  58.             $containerDeprecationLogs $this->getContainerDeprecationLogs();
  59.             $this->data $this->computeErrorsCount($containerDeprecationLogs);
  60.             // get compiler logs later (only when they are needed) to improve performance
  61.             $this->data['compiler_logs'] = [];
  62.             $this->data['compiler_logs_filepath'] = $this->containerPathPrefix.'Compiler.log';
  63.             $this->data['logs'] = $this->sanitizeLogs(array_merge($this->logger->getLogs($this->currentRequest), $containerDeprecationLogs));
  64.             $this->data $this->cloneVar($this->data);
  65.         }
  66.         $this->currentRequest null;
  67.     }
  68.     public function getLogs()
  69.     {
  70.         return isset($this->data['logs']) ? $this->data['logs'] : [];
  71.     }
  72.     public function getPriorities()
  73.     {
  74.         return isset($this->data['priorities']) ? $this->data['priorities'] : [];
  75.     }
  76.     public function countErrors()
  77.     {
  78.         return isset($this->data['error_count']) ? $this->data['error_count'] : 0;
  79.     }
  80.     public function countDeprecations()
  81.     {
  82.         return isset($this->data['deprecation_count']) ? $this->data['deprecation_count'] : 0;
  83.     }
  84.     public function countWarnings()
  85.     {
  86.         return isset($this->data['warning_count']) ? $this->data['warning_count'] : 0;
  87.     }
  88.     public function countScreams()
  89.     {
  90.         return isset($this->data['scream_count']) ? $this->data['scream_count'] : 0;
  91.     }
  92.     public function getCompilerLogs()
  93.     {
  94.         return $this->cloneVar($this->getContainerCompilerLogs($this->data['compiler_logs_filepath'] ?? null));
  95.     }
  96.     /**
  97.      * {@inheritdoc}
  98.      */
  99.     public function getName()
  100.     {
  101.         return 'logger';
  102.     }
  103.     private function getContainerDeprecationLogs()
  104.     {
  105.         if (null === $this->containerPathPrefix || !file_exists($file $this->containerPathPrefix.'Deprecations.log')) {
  106.             return [];
  107.         }
  108.         if ('' === $logContent trim(file_get_contents($file))) {
  109.             return [];
  110.         }
  111.         $bootTime filemtime($file);
  112.         $logs = [];
  113.         foreach (unserialize($logContent) as $log) {
  114.             $log['context'] = ['exception' => new SilencedErrorContext($log['type'], $log['file'], $log['line'], $log['trace'], $log['count'])];
  115.             $log['timestamp'] = $bootTime;
  116.             $log['priority'] = 100;
  117.             $log['priorityName'] = 'DEBUG';
  118.             $log['channel'] = null;
  119.             $log['scream'] = false;
  120.             unset($log['type'], $log['file'], $log['line'], $log['trace'], $log['trace'], $log['count']);
  121.             $logs[] = $log;
  122.         }
  123.         return $logs;
  124.     }
  125.     private function getContainerCompilerLogs(string $compilerLogsFilepath null): array
  126.     {
  127.         if (!file_exists($compilerLogsFilepath)) {
  128.             return [];
  129.         }
  130.         $logs = [];
  131.         foreach (file($compilerLogsFilepathFILE_IGNORE_NEW_LINES) as $log) {
  132.             $log explode(': '$log2);
  133.             if (!isset($log[1]) || !preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)++$/'$log[0])) {
  134.                 $log = ['Unknown Compiler Pass'implode(': '$log)];
  135.             }
  136.             $logs[$log[0]][] = ['message' => $log[1]];
  137.         }
  138.         return $logs;
  139.     }
  140.     private function sanitizeLogs($logs)
  141.     {
  142.         $sanitizedLogs = [];
  143.         $silencedLogs = [];
  144.         foreach ($logs as $log) {
  145.             if (!$this->isSilencedOrDeprecationErrorLog($log)) {
  146.                 $sanitizedLogs[] = $log;
  147.                 continue;
  148.             }
  149.             $message $log['message'];
  150.             $exception $log['context']['exception'];
  151.             if ($exception instanceof SilencedErrorContext) {
  152.                 if (isset($silencedLogs[$h spl_object_hash($exception)])) {
  153.                     continue;
  154.                 }
  155.                 $silencedLogs[$h] = true;
  156.                 if (!isset($sanitizedLogs[$message])) {
  157.                     $sanitizedLogs[$message] = $log + [
  158.                         'errorCount' => 0,
  159.                         'scream' => true,
  160.                     ];
  161.                 }
  162.                 $sanitizedLogs[$message]['errorCount'] += $exception->count;
  163.                 continue;
  164.             }
  165.             $errorId md5("{$exception->getSeverity()}/{$exception->getLine()}/{$exception->getFile()}\0{$message}"true);
  166.             if (isset($sanitizedLogs[$errorId])) {
  167.                 ++$sanitizedLogs[$errorId]['errorCount'];
  168.             } else {
  169.                 $log += [
  170.                     'errorCount' => 1,
  171.                     'scream' => false,
  172.                 ];
  173.                 $sanitizedLogs[$errorId] = $log;
  174.             }
  175.         }
  176.         return array_values($sanitizedLogs);
  177.     }
  178.     private function isSilencedOrDeprecationErrorLog(array $log)
  179.     {
  180.         if (!isset($log['context']['exception'])) {
  181.             return false;
  182.         }
  183.         $exception $log['context']['exception'];
  184.         if ($exception instanceof SilencedErrorContext) {
  185.             return true;
  186.         }
  187.         if ($exception instanceof \ErrorException && \in_array($exception->getSeverity(), [E_DEPRECATEDE_USER_DEPRECATED], true)) {
  188.             return true;
  189.         }
  190.         return false;
  191.     }
  192.     private function computeErrorsCount(array $containerDeprecationLogs)
  193.     {
  194.         $silencedLogs = [];
  195.         $count = [
  196.             'error_count' => $this->logger->countErrors($this->currentRequest),
  197.             'deprecation_count' => 0,
  198.             'warning_count' => 0,
  199.             'scream_count' => 0,
  200.             'priorities' => [],
  201.         ];
  202.         foreach ($this->logger->getLogs($this->currentRequest) as $log) {
  203.             if (isset($count['priorities'][$log['priority']])) {
  204.                 ++$count['priorities'][$log['priority']]['count'];
  205.             } else {
  206.                 $count['priorities'][$log['priority']] = [
  207.                     'count' => 1,
  208.                     'name' => $log['priorityName'],
  209.                 ];
  210.             }
  211.             if ('WARNING' === $log['priorityName']) {
  212.                 ++$count['warning_count'];
  213.             }
  214.             if ($this->isSilencedOrDeprecationErrorLog($log)) {
  215.                 $exception $log['context']['exception'];
  216.                 if ($exception instanceof SilencedErrorContext) {
  217.                     if (isset($silencedLogs[$h spl_object_hash($exception)])) {
  218.                         continue;
  219.                     }
  220.                     $silencedLogs[$h] = true;
  221.                     $count['scream_count'] += $exception->count;
  222.                 } else {
  223.                     ++$count['deprecation_count'];
  224.                 }
  225.             }
  226.         }
  227.         foreach ($containerDeprecationLogs as $deprecationLog) {
  228.             $count['deprecation_count'] += $deprecationLog['context']['exception']->count;
  229.         }
  230.         ksort($count['priorities']);
  231.         return $count;
  232.     }
  233. }