LoggerDataCollector.php 11 KB

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