No Description

LoggerDataCollector.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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\Response;
  14. use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
  15. /**
  16. * LogDataCollector.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. class LoggerDataCollector extends DataCollector implements LateDataCollectorInterface
  21. {
  22. private $logger;
  23. public function __construct($logger = null)
  24. {
  25. if (null !== $logger && $logger instanceof DebugLoggerInterface) {
  26. $this->logger = $logger;
  27. }
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function collect(Request $request, Response $response, \Exception $exception = null)
  33. {
  34. // everything is done as late as possible
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function lateCollect()
  40. {
  41. if (null !== $this->logger) {
  42. $this->data = $this->computeErrorsCount();
  43. $this->data['logs'] = $this->sanitizeLogs($this->logger->getLogs());
  44. }
  45. }
  46. /**
  47. * Gets the logs.
  48. *
  49. * @return array An array of logs
  50. */
  51. public function getLogs()
  52. {
  53. return isset($this->data['logs']) ? $this->data['logs'] : array();
  54. }
  55. public function getPriorities()
  56. {
  57. return isset($this->data['priorities']) ? $this->data['priorities'] : array();
  58. }
  59. public function countErrors()
  60. {
  61. return isset($this->data['error_count']) ? $this->data['error_count'] : 0;
  62. }
  63. public function countDeprecations()
  64. {
  65. return isset($this->data['deprecation_count']) ? $this->data['deprecation_count'] : 0;
  66. }
  67. public function countWarnings()
  68. {
  69. return isset($this->data['warning_count']) ? $this->data['warning_count'] : 0;
  70. }
  71. public function countScreams()
  72. {
  73. return isset($this->data['scream_count']) ? $this->data['scream_count'] : 0;
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function getName()
  79. {
  80. return 'logger';
  81. }
  82. private function sanitizeLogs($logs)
  83. {
  84. $sanitizedLogs = array();
  85. foreach ($logs as $log) {
  86. if (!$this->isSilencedOrDeprecationErrorLog($log)) {
  87. $log['context'] = $log['context'] ? $this->cloneVar($log['context']) : $log['context'];
  88. $sanitizedLogs[] = $log;
  89. continue;
  90. }
  91. $exception = $log['context']['exception'];
  92. $errorId = md5("{$exception->getSeverity()}/{$exception->getLine()}/{$exception->getFile()}".($exception instanceof \Exception ? "\0".$exception->getMessage() : ''), true);
  93. if (isset($sanitizedLogs[$errorId])) {
  94. ++$sanitizedLogs[$errorId]['errorCount'];
  95. } else {
  96. $log['context'] = $log['context'] ? $this->cloneVar($log['context']) : $log['context'];
  97. $log += array(
  98. 'errorCount' => 1,
  99. 'scream' => $exception instanceof SilencedErrorContext,
  100. );
  101. $sanitizedLogs[$errorId] = $log;
  102. }
  103. }
  104. return array_values($sanitizedLogs);
  105. }
  106. private function isSilencedOrDeprecationErrorLog(array $log)
  107. {
  108. if (!isset($log['context']['exception'])) {
  109. return false;
  110. }
  111. $exception = $log['context']['exception'];
  112. if ($exception instanceof SilencedErrorContext) {
  113. return true;
  114. }
  115. if ($exception instanceof \ErrorException && in_array($exception->getSeverity(), array(E_DEPRECATED, E_USER_DEPRECATED), true)) {
  116. return true;
  117. }
  118. return false;
  119. }
  120. private function computeErrorsCount()
  121. {
  122. $count = array(
  123. 'error_count' => $this->logger->countErrors(),
  124. 'deprecation_count' => 0,
  125. 'warning_count' => 0,
  126. 'scream_count' => 0,
  127. 'priorities' => array(),
  128. );
  129. foreach ($this->logger->getLogs() as $log) {
  130. if (isset($count['priorities'][$log['priority']])) {
  131. ++$count['priorities'][$log['priority']]['count'];
  132. } else {
  133. $count['priorities'][$log['priority']] = array(
  134. 'count' => 1,
  135. 'name' => $log['priorityName'],
  136. );
  137. }
  138. if ('WARNING' === $log['priorityName']) {
  139. ++$count['warning_count'];
  140. }
  141. if ($this->isSilencedOrDeprecationErrorLog($log)) {
  142. if ($log['context']['exception'] instanceof SilencedErrorContext) {
  143. ++$count['scream_count'];
  144. } else {
  145. ++$count['deprecation_count'];
  146. }
  147. }
  148. }
  149. ksort($count['priorities']);
  150. return $count;
  151. }
  152. }