[ Index ]

PHP Cross Reference of phpBB-3.3.2-deutsch

title

Body

[close]

/vendor/symfony/http-kernel/DataCollector/ -> LoggerDataCollector.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\HttpKernel\DataCollector;
  13  
  14  use Symfony\Component\Debug\Exception\SilencedErrorContext;
  15  use Symfony\Component\HttpFoundation\Request;
  16  use Symfony\Component\HttpFoundation\Response;
  17  use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
  18  
  19  /**
  20   * LogDataCollector.
  21   *
  22   * @author Fabien Potencier <fabien@symfony.com>
  23   */
  24  class LoggerDataCollector extends DataCollector implements LateDataCollectorInterface
  25  {
  26      private $logger;
  27      private $containerPathPrefix;
  28  
  29      public function __construct($logger = null, $containerPathPrefix = null)
  30      {
  31          if (null !== $logger && $logger instanceof DebugLoggerInterface) {
  32              if (!method_exists($logger, 'clear')) {
  33                  @trigger_error(sprintf('Implementing "%s" without the "clear()" method is deprecated since Symfony 3.4 and will be unsupported in 4.0 for class "%s".', DebugLoggerInterface::class, \get_class($logger)), \E_USER_DEPRECATED);
  34              }
  35  
  36              $this->logger = $logger;
  37          }
  38  
  39          $this->containerPathPrefix = $containerPathPrefix;
  40      }
  41  
  42      /**
  43       * {@inheritdoc}
  44       */
  45      public function collect(Request $request, Response $response, \Exception $exception = null)
  46      {
  47          // everything is done as late as possible
  48      }
  49  
  50      /**
  51       * {@inheritdoc}
  52       */
  53      public function reset()
  54      {
  55          if ($this->logger && method_exists($this->logger, 'clear')) {
  56              $this->logger->clear();
  57          }
  58          $this->data = [];
  59      }
  60  
  61      /**
  62       * {@inheritdoc}
  63       */
  64      public function lateCollect()
  65      {
  66          if (null !== $this->logger) {
  67              $containerDeprecationLogs = $this->getContainerDeprecationLogs();
  68              $this->data = $this->computeErrorsCount($containerDeprecationLogs);
  69              $this->data['compiler_logs'] = $this->getContainerCompilerLogs();
  70              $this->data['logs'] = $this->sanitizeLogs(array_merge($this->logger->getLogs(), $containerDeprecationLogs));
  71              $this->data = $this->cloneVar($this->data);
  72          }
  73      }
  74  
  75      public function getLogs()
  76      {
  77          return isset($this->data['logs']) ? $this->data['logs'] : [];
  78      }
  79  
  80      public function getPriorities()
  81      {
  82          return isset($this->data['priorities']) ? $this->data['priorities'] : [];
  83      }
  84  
  85      public function countErrors()
  86      {
  87          return isset($this->data['error_count']) ? $this->data['error_count'] : 0;
  88      }
  89  
  90      public function countDeprecations()
  91      {
  92          return isset($this->data['deprecation_count']) ? $this->data['deprecation_count'] : 0;
  93      }
  94  
  95      public function countWarnings()
  96      {
  97          return isset($this->data['warning_count']) ? $this->data['warning_count'] : 0;
  98      }
  99  
 100      public function countScreams()
 101      {
 102          return isset($this->data['scream_count']) ? $this->data['scream_count'] : 0;
 103      }
 104  
 105      public function getCompilerLogs()
 106      {
 107          return isset($this->data['compiler_logs']) ? $this->data['compiler_logs'] : [];
 108      }
 109  
 110      /**
 111       * {@inheritdoc}
 112       */
 113      public function getName()
 114      {
 115          return 'logger';
 116      }
 117  
 118      private function getContainerDeprecationLogs()
 119      {
 120          if (null === $this->containerPathPrefix || !file_exists($file = $this->containerPathPrefix.'Deprecations.log')) {
 121              return [];
 122          }
 123  
 124          if ('' === $logContent = trim(file_get_contents($file))) {
 125              return [];
 126          }
 127  
 128          $bootTime = filemtime($file);
 129          $logs = [];
 130          foreach (unserialize($logContent) as $log) {
 131              $log['context'] = ['exception' => new SilencedErrorContext($log['type'], $log['file'], $log['line'], $log['trace'], $log['count'])];
 132              $log['timestamp'] = $bootTime;
 133              $log['priority'] = 100;
 134              $log['priorityName'] = 'DEBUG';
 135              $log['channel'] = '-';
 136              $log['scream'] = false;
 137              unset($log['type'], $log['file'], $log['line'], $log['trace'], $log['trace'], $log['count']);
 138              $logs[] = $log;
 139          }
 140  
 141          return $logs;
 142      }
 143  
 144      private function getContainerCompilerLogs()
 145      {
 146          if (null === $this->containerPathPrefix || !file_exists($file = $this->containerPathPrefix.'Compiler.log')) {
 147              return [];
 148          }
 149  
 150          $logs = [];
 151          foreach (file($file, \FILE_IGNORE_NEW_LINES) as $log) {
 152              $log = explode(': ', $log, 2);
 153              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])) {
 154                  $log = ['Unknown Compiler Pass', implode(': ', $log)];
 155              }
 156  
 157              $logs[$log[0]][] = ['message' => $log[1]];
 158          }
 159  
 160          return $logs;
 161      }
 162  
 163      private function sanitizeLogs($logs)
 164      {
 165          $sanitizedLogs = [];
 166          $silencedLogs = [];
 167  
 168          foreach ($logs as $log) {
 169              if (!$this->isSilencedOrDeprecationErrorLog($log)) {
 170                  $sanitizedLogs[] = $log;
 171  
 172                  continue;
 173              }
 174  
 175              $message = '_'.$log['message'];
 176              $exception = $log['context']['exception'];
 177  
 178              if ($exception instanceof SilencedErrorContext) {
 179                  if (isset($silencedLogs[$h = spl_object_hash($exception)])) {
 180                      continue;
 181                  }
 182                  $silencedLogs[$h] = true;
 183  
 184                  if (!isset($sanitizedLogs[$message])) {
 185                      $sanitizedLogs[$message] = $log + [
 186                          'errorCount' => 0,
 187                          'scream' => true,
 188                      ];
 189                  }
 190                  $sanitizedLogs[$message]['errorCount'] += $exception->count;
 191  
 192                  continue;
 193              }
 194  
 195              $errorId = md5("{$exception->getSeverity()}/{$exception->getLine()}/{$exception->getFile()}\0{$message}", true);
 196  
 197              if (isset($sanitizedLogs[$errorId])) {
 198                  ++$sanitizedLogs[$errorId]['errorCount'];
 199              } else {
 200                  $log += [
 201                      'errorCount' => 1,
 202                      'scream' => false,
 203                  ];
 204  
 205                  $sanitizedLogs[$errorId] = $log;
 206              }
 207          }
 208  
 209          return array_values($sanitizedLogs);
 210      }
 211  
 212      private function isSilencedOrDeprecationErrorLog(array $log)
 213      {
 214          if (!isset($log['context']['exception'])) {
 215              return false;
 216          }
 217  
 218          $exception = $log['context']['exception'];
 219  
 220          if ($exception instanceof SilencedErrorContext) {
 221              return true;
 222          }
 223  
 224          if ($exception instanceof \ErrorException && \in_array($exception->getSeverity(), [\E_DEPRECATED, \E_USER_DEPRECATED], true)) {
 225              return true;
 226          }
 227  
 228          return false;
 229      }
 230  
 231      private function computeErrorsCount(array $containerDeprecationLogs)
 232      {
 233          $silencedLogs = [];
 234          $count = [
 235              'error_count' => $this->logger->countErrors(),
 236              'deprecation_count' => 0,
 237              'warning_count' => 0,
 238              'scream_count' => 0,
 239              'priorities' => [],
 240          ];
 241  
 242          foreach ($this->logger->getLogs() as $log) {
 243              if (isset($count['priorities'][$log['priority']])) {
 244                  ++$count['priorities'][$log['priority']]['count'];
 245              } else {
 246                  $count['priorities'][$log['priority']] = [
 247                      'count' => 1,
 248                      'name' => $log['priorityName'],
 249                  ];
 250              }
 251              if ('WARNING' === $log['priorityName']) {
 252                  ++$count['warning_count'];
 253              }
 254  
 255              if ($this->isSilencedOrDeprecationErrorLog($log)) {
 256                  $exception = $log['context']['exception'];
 257                  if ($exception instanceof SilencedErrorContext) {
 258                      if (isset($silencedLogs[$h = spl_object_hash($exception)])) {
 259                          continue;
 260                      }
 261                      $silencedLogs[$h] = true;
 262                      $count['scream_count'] += $exception->count;
 263                  } else {
 264                      ++$count['deprecation_count'];
 265                  }
 266              }
 267          }
 268  
 269          foreach ($containerDeprecationLogs as $deprecationLog) {
 270              $count['deprecation_count'] += $deprecationLog['context']['exception']->count;
 271          }
 272  
 273          ksort($count['priorities']);
 274  
 275          return $count;
 276      }
 277  }


Generated: Wed Nov 11 20:28:18 2020 Cross-referenced by PHPXref 0.7.1