Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LoggerDataCollector.php 8.6KB

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