LoggerDataCollector.php 7.9 KB

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