菜谱项目

LoggerDataCollector.php 7.9KB

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