LoggerDataCollector.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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\ErrorHandler\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\DebugLoggerConfigurator;
  16. use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
  17. use Symfony\Component\VarDumper\Cloner\Data;
  18. /**
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. *
  21. * @final
  22. */
  23. class LoggerDataCollector extends DataCollector implements LateDataCollectorInterface
  24. {
  25. private ?DebugLoggerInterface $logger;
  26. private ?Request $currentRequest = null;
  27. private ?array $processedLogs = null;
  28. public function __construct(
  29. ?object $logger = null,
  30. private ?string $containerPathPrefix = null,
  31. private ?RequestStack $requestStack = null,
  32. ) {
  33. $this->logger = DebugLoggerConfigurator::getDebugLogger($logger);
  34. }
  35. public function collect(Request $request, Response $response, ?\Throwable $exception = null): void
  36. {
  37. $this->currentRequest = $this->requestStack && $this->requestStack->getMainRequest() !== $request ? $request : null;
  38. }
  39. public function lateCollect(): void
  40. {
  41. if ($this->logger) {
  42. $containerDeprecationLogs = $this->getContainerDeprecationLogs();
  43. $this->data = $this->computeErrorsCount($containerDeprecationLogs);
  44. // get compiler logs later (only when they are needed) to improve performance
  45. $this->data['compiler_logs'] = [];
  46. $this->data['compiler_logs_filepath'] = $this->containerPathPrefix.'Compiler.log';
  47. $this->data['logs'] = $this->sanitizeLogs(array_merge($this->logger->getLogs($this->currentRequest), $containerDeprecationLogs));
  48. $this->data = $this->cloneVar($this->data);
  49. }
  50. $this->currentRequest = null;
  51. }
  52. public function getLogs(): Data|array
  53. {
  54. return $this->data['logs'] ?? [];
  55. }
  56. public function getProcessedLogs(): array
  57. {
  58. if (null !== $this->processedLogs) {
  59. return $this->processedLogs;
  60. }
  61. $rawLogs = $this->getLogs();
  62. if ([] === $rawLogs) {
  63. return $this->processedLogs = $rawLogs;
  64. }
  65. $logs = [];
  66. foreach ($this->getLogs()->getValue() as $rawLog) {
  67. $rawLogData = $rawLog->getValue();
  68. if ($rawLogData['priority']->getValue() > 300) {
  69. $logType = 'error';
  70. } elseif (isset($rawLogData['scream']) && false === $rawLogData['scream']->getValue()) {
  71. $logType = 'deprecation';
  72. } elseif (isset($rawLogData['scream']) && true === $rawLogData['scream']->getValue()) {
  73. $logType = 'silenced';
  74. } else {
  75. $logType = 'regular';
  76. }
  77. $logs[] = [
  78. 'type' => $logType,
  79. 'errorCount' => $rawLog['errorCount'] ?? 1,
  80. 'timestamp' => $rawLogData['timestamp_rfc3339']->getValue(),
  81. 'priority' => $rawLogData['priority']->getValue(),
  82. 'priorityName' => $rawLogData['priorityName']->getValue(),
  83. 'channel' => $rawLogData['channel']->getValue(),
  84. 'message' => $rawLogData['message'],
  85. 'context' => $rawLogData['context'],
  86. ];
  87. }
  88. // sort logs from oldest to newest
  89. usort($logs, static fn ($logA, $logB) => $logA['timestamp'] <=> $logB['timestamp']);
  90. return $this->processedLogs = $logs;
  91. }
  92. public function getFilters(): array
  93. {
  94. $filters = [
  95. 'channel' => [],
  96. 'priority' => [
  97. 'Debug' => 100,
  98. 'Info' => 200,
  99. 'Notice' => 250,
  100. 'Warning' => 300,
  101. 'Error' => 400,
  102. 'Critical' => 500,
  103. 'Alert' => 550,
  104. 'Emergency' => 600,
  105. ],
  106. ];
  107. $allChannels = [];
  108. foreach ($this->getProcessedLogs() as $log) {
  109. if ('' === trim($log['channel'] ?? '')) {
  110. continue;
  111. }
  112. $allChannels[] = $log['channel'];
  113. }
  114. $channels = array_unique($allChannels);
  115. sort($channels);
  116. $filters['channel'] = $channels;
  117. return $filters;
  118. }
  119. public function getPriorities(): Data|array
  120. {
  121. return $this->data['priorities'] ?? [];
  122. }
  123. public function countErrors(): int
  124. {
  125. return $this->data['error_count'] ?? 0;
  126. }
  127. public function countDeprecations(): int
  128. {
  129. return $this->data['deprecation_count'] ?? 0;
  130. }
  131. public function countWarnings(): int
  132. {
  133. return $this->data['warning_count'] ?? 0;
  134. }
  135. public function countScreams(): int
  136. {
  137. return $this->data['scream_count'] ?? 0;
  138. }
  139. public function getCompilerLogs(): Data
  140. {
  141. return $this->cloneVar($this->getContainerCompilerLogs($this->data['compiler_logs_filepath'] ?? null));
  142. }
  143. public function getName(): string
  144. {
  145. return 'logger';
  146. }
  147. private function getContainerDeprecationLogs(): array
  148. {
  149. if (null === $this->containerPathPrefix || !is_file($file = $this->containerPathPrefix.'Deprecations.log')) {
  150. return [];
  151. }
  152. if ('' === $logContent = trim(file_get_contents($file))) {
  153. return [];
  154. }
  155. $bootTime = filemtime($file);
  156. $logs = [];
  157. foreach (unserialize($logContent) as $log) {
  158. $log['context'] = ['exception' => new SilencedErrorContext($log['type'], $log['file'], $log['line'], $log['trace'], $log['count'])];
  159. $log['timestamp'] = $bootTime;
  160. $log['timestamp_rfc3339'] = (new \DateTimeImmutable())->setTimestamp($bootTime)->format(\DateTimeInterface::RFC3339_EXTENDED);
  161. $log['priority'] = 100;
  162. $log['priorityName'] = 'DEBUG';
  163. $log['channel'] = null;
  164. $log['scream'] = false;
  165. unset($log['type'], $log['file'], $log['line'], $log['trace'], $log['count']);
  166. $logs[] = $log;
  167. }
  168. return $logs;
  169. }
  170. private function getContainerCompilerLogs(?string $compilerLogsFilepath = null): array
  171. {
  172. if (!$compilerLogsFilepath || !is_file($compilerLogsFilepath)) {
  173. return [];
  174. }
  175. $logs = [];
  176. foreach (file($compilerLogsFilepath, \FILE_IGNORE_NEW_LINES) as $log) {
  177. $log = explode(': ', $log, 2);
  178. 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])) {
  179. $log = ['Unknown Compiler Pass', implode(': ', $log)];
  180. }
  181. $logs[$log[0]][] = ['message' => $log[1]];
  182. }
  183. return $logs;
  184. }
  185. private function sanitizeLogs(array $logs): array
  186. {
  187. $sanitizedLogs = [];
  188. $silencedLogs = [];
  189. foreach ($logs as $log) {
  190. if (!$this->isSilencedOrDeprecationErrorLog($log)) {
  191. $sanitizedLogs[] = $log;
  192. continue;
  193. }
  194. $message = '_'.$log['message'];
  195. $exception = $log['context']['exception'];
  196. if ($exception instanceof SilencedErrorContext) {
  197. if (isset($silencedLogs[$h = spl_object_hash($exception)])) {
  198. continue;
  199. }
  200. $silencedLogs[$h] = true;
  201. if (!isset($sanitizedLogs[$message])) {
  202. $sanitizedLogs[$message] = $log + [
  203. 'errorCount' => 0,
  204. 'scream' => true,
  205. ];
  206. }
  207. $sanitizedLogs[$message]['errorCount'] += $exception->count;
  208. continue;
  209. }
  210. $errorId = hash('xxh128', "{$exception->getSeverity()}/{$exception->getLine()}/{$exception->getFile()}\0{$message}", true);
  211. if (isset($sanitizedLogs[$errorId])) {
  212. ++$sanitizedLogs[$errorId]['errorCount'];
  213. } else {
  214. $log += [
  215. 'errorCount' => 1,
  216. 'scream' => false,
  217. ];
  218. $sanitizedLogs[$errorId] = $log;
  219. }
  220. }
  221. return array_values($sanitizedLogs);
  222. }
  223. private function isSilencedOrDeprecationErrorLog(array $log): bool
  224. {
  225. if (!isset($log['context']['exception'])) {
  226. return false;
  227. }
  228. $exception = $log['context']['exception'];
  229. if ($exception instanceof SilencedErrorContext) {
  230. return true;
  231. }
  232. if ($exception instanceof \ErrorException && \in_array($exception->getSeverity(), [\E_DEPRECATED, \E_USER_DEPRECATED], true)) {
  233. return true;
  234. }
  235. return false;
  236. }
  237. private function computeErrorsCount(array $containerDeprecationLogs): array
  238. {
  239. $silencedLogs = [];
  240. $count = [
  241. 'error_count' => $this->logger->countErrors($this->currentRequest),
  242. 'deprecation_count' => 0,
  243. 'warning_count' => 0,
  244. 'scream_count' => 0,
  245. 'priorities' => [],
  246. ];
  247. foreach ($this->logger->getLogs($this->currentRequest) as $log) {
  248. if (isset($count['priorities'][$log['priority']])) {
  249. ++$count['priorities'][$log['priority']]['count'];
  250. } else {
  251. $count['priorities'][$log['priority']] = [
  252. 'count' => 1,
  253. 'name' => $log['priorityName'],
  254. ];
  255. }
  256. if ('WARNING' === $log['priorityName']) {
  257. ++$count['warning_count'];
  258. }
  259. if ($this->isSilencedOrDeprecationErrorLog($log)) {
  260. $exception = $log['context']['exception'];
  261. if ($exception instanceof SilencedErrorContext) {
  262. if (isset($silencedLogs[$h = spl_object_hash($exception)])) {
  263. continue;
  264. }
  265. $silencedLogs[$h] = true;
  266. $count['scream_count'] += $exception->count;
  267. } else {
  268. ++$count['deprecation_count'];
  269. }
  270. }
  271. }
  272. foreach ($containerDeprecationLogs as $deprecationLog) {
  273. $count['deprecation_count'] += $deprecationLog['context']['exception']->count;
  274. }
  275. ksort($count['priorities']);
  276. return $count;
  277. }
  278. }