PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/susmitha.plts/photographer_portfolio
PHP | 216 lines | 147 code | 34 blank | 35 comment | 20 complexity | 8e621195f60cff7a4626b11dbe5b53e3 MD5 | raw file
  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\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
  14. /**
  15. * LogDataCollector.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class LoggerDataCollector extends DataCollector implements LateDataCollectorInterface
  20. {
  21. private $errorNames = array(
  22. E_DEPRECATED => 'E_DEPRECATED',
  23. E_USER_DEPRECATED => 'E_USER_DEPRECATED',
  24. E_NOTICE => 'E_NOTICE',
  25. E_USER_NOTICE => 'E_USER_NOTICE',
  26. E_STRICT => 'E_STRICT',
  27. E_WARNING => 'E_WARNING',
  28. E_USER_WARNING => 'E_USER_WARNING',
  29. E_COMPILE_WARNING => 'E_COMPILE_WARNING',
  30. E_CORE_WARNING => 'E_CORE_WARNING',
  31. E_USER_ERROR => 'E_USER_ERROR',
  32. E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
  33. E_COMPILE_ERROR => 'E_COMPILE_ERROR',
  34. E_PARSE => 'E_PARSE',
  35. E_ERROR => 'E_ERROR',
  36. E_CORE_ERROR => 'E_CORE_ERROR',
  37. );
  38. private $logger;
  39. public function __construct($logger = null)
  40. {
  41. if (null !== $logger && $logger instanceof DebugLoggerInterface) {
  42. $this->logger = $logger;
  43. }
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function collect(Request $request, Response $response, \Exception $exception = null)
  49. {
  50. // everything is done as late as possible
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function lateCollect()
  56. {
  57. if (null !== $this->logger) {
  58. $this->data = $this->computeErrorsCount();
  59. $this->data['logs'] = $this->sanitizeLogs($this->logger->getLogs());
  60. }
  61. }
  62. /**
  63. * Gets the called events.
  64. *
  65. * @return array An array of called events
  66. *
  67. * @see TraceableEventDispatcherInterface
  68. */
  69. public function countErrors()
  70. {
  71. return isset($this->data['error_count']) ? $this->data['error_count'] : 0;
  72. }
  73. /**
  74. * Gets the logs.
  75. *
  76. * @return array An array of logs
  77. */
  78. public function getLogs()
  79. {
  80. return isset($this->data['logs']) ? $this->data['logs'] : array();
  81. }
  82. public function getPriorities()
  83. {
  84. return isset($this->data['priorities']) ? $this->data['priorities'] : array();
  85. }
  86. public function countDeprecations()
  87. {
  88. return isset($this->data['deprecation_count']) ? $this->data['deprecation_count'] : 0;
  89. }
  90. public function countScreams()
  91. {
  92. return isset($this->data['scream_count']) ? $this->data['scream_count'] : 0;
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function getName()
  98. {
  99. return 'logger';
  100. }
  101. private function sanitizeLogs($logs)
  102. {
  103. $errorContextById = array();
  104. $sanitizedLogs = array();
  105. foreach ($logs as $log) {
  106. $context = $this->sanitizeContext($log['context']);
  107. if (isset($context['type'], $context['file'], $context['line'], $context['level'])) {
  108. $errorId = md5("{$context['type']}/{$context['line']}/{$context['file']}\x00{$log['message']}", true);
  109. $silenced = !($context['type'] & $context['level']);
  110. if (isset($this->errorNames[$context['type']])) {
  111. $context = array_merge(array('name' => $this->errorNames[$context['type']]), $context);
  112. }
  113. if (isset($errorContextById[$errorId])) {
  114. if (isset($errorContextById[$errorId]['errorCount'])) {
  115. ++$errorContextById[$errorId]['errorCount'];
  116. } else {
  117. $errorContextById[$errorId]['errorCount'] = 2;
  118. }
  119. if (!$silenced && isset($errorContextById[$errorId]['scream'])) {
  120. unset($errorContextById[$errorId]['scream']);
  121. $errorContextById[$errorId]['level'] = $context['level'];
  122. }
  123. continue;
  124. }
  125. $errorContextById[$errorId] = &$context;
  126. if ($silenced) {
  127. $context['scream'] = true;
  128. }
  129. $log['context'] = &$context;
  130. unset($context);
  131. } else {
  132. $log['context'] = $context;
  133. }
  134. $sanitizedLogs[] = $log;
  135. }
  136. return $sanitizedLogs;
  137. }
  138. private function sanitizeContext($context)
  139. {
  140. if (is_array($context)) {
  141. foreach ($context as $key => $value) {
  142. $context[$key] = $this->sanitizeContext($value);
  143. }
  144. return $context;
  145. }
  146. if (is_resource($context)) {
  147. return sprintf('Resource(%s)', get_resource_type($context));
  148. }
  149. if (is_object($context)) {
  150. return sprintf('Object(%s)', get_class($context));
  151. }
  152. return $context;
  153. }
  154. private function computeErrorsCount()
  155. {
  156. $count = array(
  157. 'error_count' => $this->logger->countErrors(),
  158. 'deprecation_count' => 0,
  159. 'scream_count' => 0,
  160. 'priorities' => array(),
  161. );
  162. foreach ($this->logger->getLogs() as $log) {
  163. if (isset($count['priorities'][$log['priority']])) {
  164. ++$count['priorities'][$log['priority']]['count'];
  165. } else {
  166. $count['priorities'][$log['priority']] = array(
  167. 'count' => 1,
  168. 'name' => $log['priorityName'],
  169. );
  170. }
  171. if (isset($log['context']['type'], $log['context']['level'])) {
  172. if (E_DEPRECATED === $log['context']['type'] || E_USER_DEPRECATED === $log['context']['type']) {
  173. ++$count['deprecation_count'];
  174. } elseif (!($log['context']['type'] & $log['context']['level'])) {
  175. ++$count['scream_count'];
  176. }
  177. }
  178. }
  179. ksort($count['priorities']);
  180. return $count;
  181. }
  182. }