LoggerDataCollector.php 8.5KB

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