LoggerDataCollector.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. if ('' === $logContent = trim(file_get_contents($file))) {
  110. return [];
  111. }
  112. $bootTime = filemtime($file);
  113. $logs = [];
  114. foreach (unserialize($logContent) as $log) {
  115. $log['context'] = ['exception' => new SilencedErrorContext($log['type'], $log['file'], $log['line'], $log['trace'], $log['count'])];
  116. $log['timestamp'] = $bootTime;
  117. $log['priority'] = 100;
  118. $log['priorityName'] = 'DEBUG';
  119. $log['channel'] = '-';
  120. $log['scream'] = false;
  121. unset($log['type'], $log['file'], $log['line'], $log['trace'], $log['trace'], $log['count']);
  122. $logs[] = $log;
  123. }
  124. return $logs;
  125. }
  126. private function getContainerCompilerLogs()
  127. {
  128. if (null === $this->containerPathPrefix || !file_exists($file = $this->containerPathPrefix.'Compiler.log')) {
  129. return [];
  130. }
  131. $logs = [];
  132. foreach (file($file, FILE_IGNORE_NEW_LINES) as $log) {
  133. $log = explode(': ', $log, 2);
  134. 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])) {
  135. $log = ['Unknown Compiler Pass', implode(': ', $log)];
  136. }
  137. $logs[$log[0]][] = ['message' => $log[1]];
  138. }
  139. return $logs;
  140. }
  141. private function sanitizeLogs($logs)
  142. {
  143. $sanitizedLogs = [];
  144. $silencedLogs = [];
  145. foreach ($logs as $log) {
  146. if (!$this->isSilencedOrDeprecationErrorLog($log)) {
  147. $sanitizedLogs[] = $log;
  148. continue;
  149. }
  150. $message = $log['message'];
  151. $exception = $log['context']['exception'];
  152. if ($exception instanceof SilencedErrorContext) {
  153. if (isset($silencedLogs[$h = spl_object_hash($exception)])) {
  154. continue;
  155. }
  156. $silencedLogs[$h] = true;
  157. if (!isset($sanitizedLogs[$message])) {
  158. $sanitizedLogs[$message] = $log + [
  159. 'errorCount' => 0,
  160. 'scream' => true,
  161. ];
  162. }
  163. $sanitizedLogs[$message]['errorCount'] += $exception->count;
  164. continue;
  165. }
  166. $errorId = md5("{$exception->getSeverity()}/{$exception->getLine()}/{$exception->getFile()}\0{$message}", true);
  167. if (isset($sanitizedLogs[$errorId])) {
  168. ++$sanitizedLogs[$errorId]['errorCount'];
  169. } else {
  170. $log += [
  171. 'errorCount' => 1,
  172. 'scream' => false,
  173. ];
  174. $sanitizedLogs[$errorId] = $log;
  175. }
  176. }
  177. return array_values($sanitizedLogs);
  178. }
  179. private function isSilencedOrDeprecationErrorLog(array $log)
  180. {
  181. if (!isset($log['context']['exception'])) {
  182. return false;
  183. }
  184. $exception = $log['context']['exception'];
  185. if ($exception instanceof SilencedErrorContext) {
  186. return true;
  187. }
  188. if ($exception instanceof \ErrorException && \in_array($exception->getSeverity(), [E_DEPRECATED, E_USER_DEPRECATED], true)) {
  189. return true;
  190. }
  191. return false;
  192. }
  193. private function computeErrorsCount(array $containerDeprecationLogs)
  194. {
  195. $silencedLogs = [];
  196. $count = [
  197. 'error_count' => $this->logger->countErrors(),
  198. 'deprecation_count' => 0,
  199. 'warning_count' => 0,
  200. 'scream_count' => 0,
  201. 'priorities' => [],
  202. ];
  203. foreach ($this->logger->getLogs() as $log) {
  204. if (isset($count['priorities'][$log['priority']])) {
  205. ++$count['priorities'][$log['priority']]['count'];
  206. } else {
  207. $count['priorities'][$log['priority']] = [
  208. 'count' => 1,
  209. 'name' => $log['priorityName'],
  210. ];
  211. }
  212. if ('WARNING' === $log['priorityName']) {
  213. ++$count['warning_count'];
  214. }
  215. if ($this->isSilencedOrDeprecationErrorLog($log)) {
  216. $exception = $log['context']['exception'];
  217. if ($exception instanceof SilencedErrorContext) {
  218. if (isset($silencedLogs[$h = spl_object_hash($exception)])) {
  219. continue;
  220. }
  221. $silencedLogs[$h] = true;
  222. $count['scream_count'] += $exception->count;
  223. } else {
  224. ++$count['deprecation_count'];
  225. }
  226. }
  227. }
  228. foreach ($containerDeprecationLogs as $deprecationLog) {
  229. $count['deprecation_count'] += $deprecationLog['context']['exception']->count;
  230. }
  231. ksort($count['priorities']);
  232. return $count;
  233. }
  234. }