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.             $this->data['compiler_logs'] = $this->getContainerCompilerLogs();
  61.             $this->data['logs'] = $this->sanitizeLogs(array_merge($this->logger->getLogs($this->currentRequest), $containerDeprecationLogs));
  62.             $this->data $this->cloneVar($this->data);
  63.         }
  64.         $this->currentRequest null;
  65.     }
  66.     /**
  67.      * Gets the logs.
  68.      *
  69.      * @return array An array of logs
  70.      */
  71.     public function getLogs()
  72.     {
  73.         return isset($this->data['logs']) ? $this->data['logs'] : [];
  74.     }
  75.     public function getPriorities()
  76.     {
  77.         return isset($this->data['priorities']) ? $this->data['priorities'] : [];
  78.     }
  79.     public function countErrors()
  80.     {
  81.         return isset($this->data['error_count']) ? $this->data['error_count'] : 0;
  82.     }
  83.     public function countDeprecations()
  84.     {
  85.         return isset($this->data['deprecation_count']) ? $this->data['deprecation_count'] : 0;
  86.     }
  87.     public function countWarnings()
  88.     {
  89.         return isset($this->data['warning_count']) ? $this->data['warning_count'] : 0;
  90.     }
  91.     public function countScreams()
  92.     {
  93.         return isset($this->data['scream_count']) ? $this->data['scream_count'] : 0;
  94.     }
  95.     public function getCompilerLogs()
  96.     {
  97.         return isset($this->data['compiler_logs']) ? $this->data['compiler_logs'] : [];
  98.     }
  99.     /**
  100.      * {@inheritdoc}
  101.      */
  102.     public function getName()
  103.     {
  104.         return 'logger';
  105.     }
  106.     private function getContainerDeprecationLogs()
  107.     {
  108.         if (null === $this->containerPathPrefix || !file_exists($file $this->containerPathPrefix.'Deprecations.log')) {
  109.             return [];
  110.         }
  111.         if ('' === $logContent trim(file_get_contents($file))) {
  112.             return [];
  113.         }
  114.         $bootTime filemtime($file);
  115.         $logs = [];
  116.         foreach (unserialize($logContent) as $log) {
  117.             $log['context'] = ['exception' => new SilencedErrorContext($log['type'], $log['file'], $log['line'], $log['trace'], $log['count'])];
  118.             $log['timestamp'] = $bootTime;
  119.             $log['priority'] = 100;
  120.             $log['priorityName'] = 'DEBUG';
  121.             $log['channel'] = null;
  122.             $log['scream'] = false;
  123.             unset($log['type'], $log['file'], $log['line'], $log['trace'], $log['trace'], $log['count']);
  124.             $logs[] = $log;
  125.         }
  126.         return $logs;
  127.     }
  128.     private function getContainerCompilerLogs()
  129.     {
  130.         if (null === $this->containerPathPrefix || !file_exists($file $this->containerPathPrefix.'Compiler.log')) {
  131.             return [];
  132.         }
  133.         $logs = [];
  134.         foreach (file($fileFILE_IGNORE_NEW_LINES) as $log) {
  135.             $log explode(': '$log2);
  136.             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])) {
  137.                 $log = ['Unknown Compiler Pass'implode(': '$log)];
  138.             }
  139.             $logs[$log[0]][] = ['message' => $log[1]];
  140.         }
  141.         return $logs;
  142.     }
  143.     private function sanitizeLogs($logs)
  144.     {
  145.         $sanitizedLogs = [];
  146.         $silencedLogs = [];
  147.         foreach ($logs as $log) {
  148.             if (!$this->isSilencedOrDeprecationErrorLog($log)) {
  149.                 $sanitizedLogs[] = $log;
  150.                 continue;
  151.             }
  152.             $message $log['message'];
  153.             $exception $log['context']['exception'];
  154.             if ($exception instanceof SilencedErrorContext) {
  155.                 if (isset($silencedLogs[$h spl_object_hash($exception)])) {
  156.                     continue;
  157.                 }
  158.                 $silencedLogs[$h] = true;
  159.                 if (!isset($sanitizedLogs[$message])) {
  160.                     $sanitizedLogs[$message] = $log + [
  161.                         'errorCount' => 0,
  162.                         'scream' => true,
  163.                     ];
  164.                 }
  165.                 $sanitizedLogs[$message]['errorCount'] += $exception->count;
  166.                 continue;
  167.             }
  168.             $errorId md5("{$exception->getSeverity()}/{$exception->getLine()}/{$exception->getFile()}\0{$message}"true);
  169.             if (isset($sanitizedLogs[$errorId])) {
  170.                 ++$sanitizedLogs[$errorId]['errorCount'];
  171.             } else {
  172.                 $log += [
  173.                     'errorCount' => 1,
  174.                     'scream' => false,
  175.                 ];
  176.                 $sanitizedLogs[$errorId] = $log;
  177.             }
  178.         }
  179.         return array_values($sanitizedLogs);
  180.     }
  181.     private function isSilencedOrDeprecationErrorLog(array $log)
  182.     {
  183.         if (!isset($log['context']['exception'])) {
  184.             return false;
  185.         }
  186.         $exception $log['context']['exception'];
  187.         if ($exception instanceof SilencedErrorContext) {
  188.             return true;
  189.         }
  190.         if ($exception instanceof \ErrorException && \in_array($exception->getSeverity(), [E_DEPRECATEDE_USER_DEPRECATED], true)) {
  191.             return true;
  192.         }
  193.         return false;
  194.     }
  195.     private function computeErrorsCount(array $containerDeprecationLogs)
  196.     {
  197.         $silencedLogs = [];
  198.         $count = [
  199.             'error_count' => $this->logger->countErrors($this->currentRequest),
  200.             'deprecation_count' => 0,
  201.             'warning_count' => 0,
  202.             'scream_count' => 0,
  203.             'priorities' => [],
  204.         ];
  205.         foreach ($this->logger->getLogs($this->currentRequest) as $log) {
  206.             if (isset($count['priorities'][$log['priority']])) {
  207.                 ++$count['priorities'][$log['priority']]['count'];
  208.             } else {
  209.                 $count['priorities'][$log['priority']] = [
  210.                     'count' => 1,
  211.                     'name' => $log['priorityName'],
  212.                 ];
  213.             }
  214.             if ('WARNING' === $log['priorityName']) {
  215.                 ++$count['warning_count'];
  216.             }
  217.             if ($this->isSilencedOrDeprecationErrorLog($log)) {
  218.                 $exception $log['context']['exception'];
  219.                 if ($exception instanceof SilencedErrorContext) {
  220.                     if (isset($silencedLogs[$h spl_object_hash($exception)])) {
  221.                         continue;
  222.                     }
  223.                     $silencedLogs[$h] = true;
  224.                     $count['scream_count'] += $exception->count;
  225.                 } else {
  226.                     ++$count['deprecation_count'];
  227.                 }
  228.             }
  229.         }
  230.         foreach ($containerDeprecationLogs as $deprecationLog) {
  231.             $count['deprecation_count'] += $deprecationLog['context']['exception']->count;
  232.         }
  233.         ksort($count['priorities']);
  234.         return $count;
  235.     }
  236. }