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
28 public function __construct($logger = null)
30 if (null !== $logger && $logger instanceof DebugLoggerInterface) {
31 $this->logger = $logger;
38 public function collect(Request $request, Response $response, \Exception $exception = null)
40 // everything is done as late as possible
46 public function lateCollect()
48 if (null !== $this->logger) {
49 $this->data = $this->computeErrorsCount();
50 $this->data['logs'] = $this->sanitizeLogs($this->logger->getLogs());
57 * @return array An array of logs
59 public function getLogs()
61 return isset($this->data['logs']) ? $this->data['logs'] : array();
64 public function getPriorities()
66 return isset($this->data['priorities']) ? $this->data['priorities'] : array();
69 public function countErrors()
71 return isset($this->data['error_count']) ? $this->data['error_count'] : 0;
74 public function countDeprecations()
76 return isset($this->data['deprecation_count']) ? $this->data['deprecation_count'] : 0;
79 public function countWarnings()
81 return isset($this->data['warning_count']) ? $this->data['warning_count'] : 0;
84 public function countScreams()
86 return isset($this->data['scream_count']) ? $this->data['scream_count'] : 0;
92 public function getName()
97 private function sanitizeLogs($logs)
99 $sanitizedLogs = array();
101 foreach ($logs as $log) {
102 if (!$this->isSilencedOrDeprecationErrorLog($log)) {
103 $log['context'] = $log['context'] ? $this->cloneVar($log['context']) : $log['context'];
104 $sanitizedLogs[] = $log;
109 $exception = $log['context']['exception'];
110 $errorId = md5("{$exception->getSeverity()}/{$exception->getLine()}/{$exception->getFile()}".($exception instanceof \Exception ? "\0".$exception->getMessage() : ''), true);
112 if (isset($sanitizedLogs[$errorId])) {
113 ++$sanitizedLogs[$errorId]['errorCount'];
115 $log['context'] = $log['context'] ? $this->cloneVar($log['context']) : $log['context'];
119 'scream' => $exception instanceof SilencedErrorContext,
122 $sanitizedLogs[$errorId] = $log;
126 return array_values($sanitizedLogs);
129 private function isSilencedOrDeprecationErrorLog(array $log)
131 if (!isset($log['context']['exception'])) {
135 $exception = $log['context']['exception'];
137 if ($exception instanceof SilencedErrorContext) {
141 if ($exception instanceof \ErrorException && in_array($exception->getSeverity(), array(E_DEPRECATED, E_USER_DEPRECATED), true)) {
148 private function computeErrorsCount()
151 'error_count' => $this->logger->countErrors(),
152 'deprecation_count' => 0,
153 'warning_count' => 0,
155 'priorities' => array(),
158 foreach ($this->logger->getLogs() as $log) {
159 if (isset($count['priorities'][$log['priority']])) {
160 ++$count['priorities'][$log['priority']]['count'];
162 $count['priorities'][$log['priority']] = array(
164 'name' => $log['priorityName'],
167 if ('WARNING' === $log['priorityName']) {
168 ++$count['warning_count'];
171 if ($this->isSilencedOrDeprecationErrorLog($log)) {
172 if ($log['context']['exception'] instanceof SilencedErrorContext) {
173 ++$count['scream_count'];
175 ++$count['deprecation_count'];
180 ksort($count['priorities']);