LoggerDataCollector.php 8.3 KB

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