4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\DataCollector;
14 use Symfony\Component\Debug\Exception\SilencedErrorContext;
15 use Symfony\Component\HttpFoundation\Request;
16 use Symfony\Component\HttpFoundation\Response;
17 use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
22 * @author Fabien Potencier <fabien@symfony.com>
24 class LoggerDataCollector extends DataCollector implements LateDataCollectorInterface
27 private $containerPathPrefix;
29 public function __construct($logger = null, $containerPathPrefix = null)
31 if (null !== $logger && $logger instanceof DebugLoggerInterface) {
32 if (!method_exists($logger, 'clear')) {
33 @trigger_error(sprintf('Implementing "%s" without the "clear()" method is deprecated since Symfony 3.4 and will be unsupported in 4.0 for class "%s".', DebugLoggerInterface::class, \get_class($logger)), E_USER_DEPRECATED);
36 $this->logger = $logger;
39 $this->containerPathPrefix = $containerPathPrefix;
45 public function collect(Request $request, Response $response, \Exception $exception = null)
47 // everything is done as late as possible
53 public function reset()
55 if ($this->logger && method_exists($this->logger, 'clear')) {
56 $this->logger->clear();
58 $this->data = array();
64 public function lateCollect()
66 if (null !== $this->logger) {
67 $containerDeprecationLogs = $this->getContainerDeprecationLogs();
68 $this->data = $this->computeErrorsCount($containerDeprecationLogs);
69 $this->data['compiler_logs'] = $this->getContainerCompilerLogs();
70 $this->data['logs'] = $this->sanitizeLogs(array_merge($this->logger->getLogs(), $containerDeprecationLogs));
71 $this->data = $this->cloneVar($this->data);
78 * @return array An array of logs
80 public function getLogs()
82 return isset($this->data['logs']) ? $this->data['logs'] : array();
85 public function getPriorities()
87 return isset($this->data['priorities']) ? $this->data['priorities'] : array();
90 public function countErrors()
92 return isset($this->data['error_count']) ? $this->data['error_count'] : 0;
95 public function countDeprecations()
97 return isset($this->data['deprecation_count']) ? $this->data['deprecation_count'] : 0;
100 public function countWarnings()
102 return isset($this->data['warning_count']) ? $this->data['warning_count'] : 0;
105 public function countScreams()
107 return isset($this->data['scream_count']) ? $this->data['scream_count'] : 0;
110 public function getCompilerLogs()
112 return isset($this->data['compiler_logs']) ? $this->data['compiler_logs'] : array();
118 public function getName()
123 private function getContainerDeprecationLogs()
125 if (null === $this->containerPathPrefix || !file_exists($file = $this->containerPathPrefix.'Deprecations.log')) {
129 $bootTime = filemtime($file);
131 foreach (unserialize(file_get_contents($file)) as $log) {
132 $log['context'] = array('exception' => new SilencedErrorContext($log['type'], $log['file'], $log['line'], $log['trace'], $log['count']));
133 $log['timestamp'] = $bootTime;
134 $log['priority'] = 100;
135 $log['priorityName'] = 'DEBUG';
136 $log['channel'] = '-';
137 $log['scream'] = false;
138 unset($log['type'], $log['file'], $log['line'], $log['trace'], $log['trace'], $log['count']);
145 private function getContainerCompilerLogs()
147 if (null === $this->containerPathPrefix || !file_exists($file = $this->containerPathPrefix.'Compiler.log')) {
152 foreach (file($file, FILE_IGNORE_NEW_LINES) as $log) {
153 $log = explode(': ', $log, 2);
154 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])) {
155 $log = array('Unknown Compiler Pass', implode(': ', $log));
158 $logs[$log[0]][] = array('message' => $log[1]);
164 private function sanitizeLogs($logs)
166 $sanitizedLogs = array();
167 $silencedLogs = array();
169 foreach ($logs as $log) {
170 if (!$this->isSilencedOrDeprecationErrorLog($log)) {
171 $sanitizedLogs[] = $log;
176 $message = $log['message'];
177 $exception = $log['context']['exception'];
179 if ($exception instanceof SilencedErrorContext) {
180 if (isset($silencedLogs[$h = spl_object_hash($exception)])) {
183 $silencedLogs[$h] = true;
185 if (!isset($sanitizedLogs[$message])) {
186 $sanitizedLogs[$message] = $log + array(
191 $sanitizedLogs[$message]['errorCount'] += $exception->count;
196 $errorId = md5("{$exception->getSeverity()}/{$exception->getLine()}/{$exception->getFile()}\0{$message}", true);
198 if (isset($sanitizedLogs[$errorId])) {
199 ++$sanitizedLogs[$errorId]['errorCount'];
206 $sanitizedLogs[$errorId] = $log;
210 return array_values($sanitizedLogs);
213 private function isSilencedOrDeprecationErrorLog(array $log)
215 if (!isset($log['context']['exception'])) {
219 $exception = $log['context']['exception'];
221 if ($exception instanceof SilencedErrorContext) {
225 if ($exception instanceof \ErrorException && \in_array($exception->getSeverity(), array(E_DEPRECATED, E_USER_DEPRECATED), true)) {
232 private function computeErrorsCount(array $containerDeprecationLogs)
234 $silencedLogs = array();
236 'error_count' => $this->logger->countErrors(),
237 'deprecation_count' => 0,
238 'warning_count' => 0,
240 'priorities' => array(),
243 foreach ($this->logger->getLogs() as $log) {
244 if (isset($count['priorities'][$log['priority']])) {
245 ++$count['priorities'][$log['priority']]['count'];
247 $count['priorities'][$log['priority']] = array(
249 'name' => $log['priorityName'],
252 if ('WARNING' === $log['priorityName']) {
253 ++$count['warning_count'];
256 if ($this->isSilencedOrDeprecationErrorLog($log)) {
257 $exception = $log['context']['exception'];
258 if ($exception instanceof SilencedErrorContext) {
259 if (isset($silencedLogs[$h = spl_object_hash($exception)])) {
262 $silencedLogs[$h] = true;
263 $count['scream_count'] += $exception->count;
265 ++$count['deprecation_count'];
270 foreach ($containerDeprecationLogs as $deprecationLog) {
271 $count['deprecation_count'] += $deprecationLog['context']['exception']->count;
274 ksort($count['priorities']);