LoggerDataCollector.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. private $containerPathPrefix;
  24. public function __construct($logger = null, $containerPathPrefix = null)
  25. {
  26. if (null !== $logger && $logger instanceof DebugLoggerInterface) {
  27. $this->logger = $logger;
  28. }
  29. $this->containerPathPrefix = $containerPathPrefix;
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function collect(Request $request, Response $response, \Exception $exception = null)
  35. {
  36. // everything is done as late as possible
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function lateCollect()
  42. {
  43. if (null !== $this->logger) {
  44. $this->data = $this->computeErrorsCount();
  45. $containerDeprecationLogs = $this->getContainerDeprecationLogs();
  46. $this->data['deprecation_count'] += count($containerDeprecationLogs);
  47. $this->data['compiler_logs'] = $this->getContainerCompilerLogs();
  48. $this->data['logs'] = $this->sanitizeLogs(array_merge($this->logger->getLogs(), $containerDeprecationLogs));
  49. $this->data = $this->cloneVar($this->data);
  50. }
  51. }
  52. /**
  53. * Gets the logs.
  54. *
  55. * @return array An array of logs
  56. */
  57. public function getLogs()
  58. {
  59. return isset($this->data['logs']) ? $this->data['logs'] : array();
  60. }
  61. public function getPriorities()
  62. {
  63. return isset($this->data['priorities']) ? $this->data['priorities'] : array();
  64. }
  65. public function countErrors()
  66. {
  67. return isset($this->data['error_count']) ? $this->data['error_count'] : 0;
  68. }
  69. public function countDeprecations()
  70. {
  71. return isset($this->data['deprecation_count']) ? $this->data['deprecation_count'] : 0;
  72. }
  73. public function countWarnings()
  74. {
  75. return isset($this->data['warning_count']) ? $this->data['warning_count'] : 0;
  76. }
  77. public function countScreams()
  78. {
  79. return isset($this->data['scream_count']) ? $this->data['scream_count'] : 0;
  80. }
  81. public function getCompilerLogs()
  82. {
  83. return isset($this->data['compiler_logs']) ? $this->data['compiler_logs'] : array();
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function getName()
  89. {
  90. return 'logger';
  91. }
  92. private function getContainerDeprecationLogs()
  93. {
  94. if (null === $this->containerPathPrefix || !file_exists($file = $this->containerPathPrefix.'Deprecations.log')) {
  95. return array();
  96. }
  97. $stubs = array();
  98. $bootTime = filemtime($file);
  99. $logs = array();
  100. foreach (unserialize(file_get_contents($file)) as $log) {
  101. $log['context'] = array('exception' => new SilencedErrorContext($log['type'], $log['file'], $log['line']));
  102. $log['timestamp'] = $bootTime;
  103. $log['priority'] = 100;
  104. $log['priorityName'] = 'DEBUG';
  105. $log['channel'] = '-';
  106. $log['scream'] = false;
  107. $logs[] = $log;
  108. }
  109. return $logs;
  110. }
  111. private function getContainerCompilerLogs()
  112. {
  113. if (null === $this->containerPathPrefix || !file_exists($file = $this->containerPathPrefix.'Compiler.log')) {
  114. return array();
  115. }
  116. $logs = array();
  117. foreach (file($file, FILE_IGNORE_NEW_LINES) as $log) {
  118. $log = explode(': ', $log, 2);
  119. 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])) {
  120. $log = array('Unknown Compiler Pass', implode(': ', $log));
  121. }
  122. $logs[$log[0]][] = array('message' => $log[1]);
  123. }
  124. return $logs;
  125. }
  126. private function sanitizeLogs($logs)
  127. {
  128. $sanitizedLogs = array();
  129. foreach ($logs as $log) {
  130. if (!$this->isSilencedOrDeprecationErrorLog($log)) {
  131. $sanitizedLogs[] = $log;
  132. continue;
  133. }
  134. $exception = $log['context']['exception'];
  135. $errorId = md5("{$exception->getSeverity()}/{$exception->getLine()}/{$exception->getFile()}\0{$log['message']}", true);
  136. if (isset($sanitizedLogs[$errorId])) {
  137. ++$sanitizedLogs[$errorId]['errorCount'];
  138. } else {
  139. $log += array(
  140. 'errorCount' => 1,
  141. 'scream' => $exception instanceof SilencedErrorContext,
  142. );
  143. $sanitizedLogs[$errorId] = $log;
  144. }
  145. }
  146. return array_values($sanitizedLogs);
  147. }
  148. private function isSilencedOrDeprecationErrorLog(array $log)
  149. {
  150. if (!isset($log['context']['exception'])) {
  151. return false;
  152. }
  153. $exception = $log['context']['exception'];
  154. if ($exception instanceof SilencedErrorContext) {
  155. return true;
  156. }
  157. if ($exception instanceof \ErrorException && in_array($exception->getSeverity(), array(E_DEPRECATED, E_USER_DEPRECATED), true)) {
  158. return true;
  159. }
  160. return false;
  161. }
  162. private function computeErrorsCount()
  163. {
  164. $count = array(
  165. 'error_count' => $this->logger->countErrors(),
  166. 'deprecation_count' => 0,
  167. 'warning_count' => 0,
  168. 'scream_count' => 0,
  169. 'priorities' => array(),
  170. );
  171. foreach ($this->logger->getLogs() as $log) {
  172. if (isset($count['priorities'][$log['priority']])) {
  173. ++$count['priorities'][$log['priority']]['count'];
  174. } else {
  175. $count['priorities'][$log['priority']] = array(
  176. 'count' => 1,
  177. 'name' => $log['priorityName'],
  178. );
  179. }
  180. if ('WARNING' === $log['priorityName']) {
  181. ++$count['warning_count'];
  182. }
  183. if ($this->isSilencedOrDeprecationErrorLog($log)) {
  184. if ($log['context']['exception'] instanceof SilencedErrorContext) {
  185. ++$count['scream_count'];
  186. } else {
  187. ++$count['deprecation_count'];
  188. }
  189. }
  190. }
  191. ksort($count['priorities']);
  192. return $count;
  193. }
  194. }