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\HttpFoundation\Request;
15 use Symfony\Component\HttpFoundation\Response;
16 use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
21 * @author Fabien Potencier <fabien@symfony.com>
23 class LoggerDataCollector extends DataCollector implements LateDataCollectorInterface
25 private $errorNames = array(
26 E_DEPRECATED => 'E_DEPRECATED',
27 E_USER_DEPRECATED => 'E_USER_DEPRECATED',
28 E_NOTICE => 'E_NOTICE',
29 E_USER_NOTICE => 'E_USER_NOTICE',
30 E_STRICT => 'E_STRICT',
31 E_WARNING => 'E_WARNING',
32 E_USER_WARNING => 'E_USER_WARNING',
33 E_COMPILE_WARNING => 'E_COMPILE_WARNING',
34 E_CORE_WARNING => 'E_CORE_WARNING',
35 E_USER_ERROR => 'E_USER_ERROR',
36 E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
37 E_COMPILE_ERROR => 'E_COMPILE_ERROR',
40 E_CORE_ERROR => 'E_CORE_ERROR',
45 public function __construct($logger = null)
47 if (null !== $logger && $logger instanceof DebugLoggerInterface) {
48 $this->logger = $logger;
55 public function collect(Request $request, Response $response, \Exception $exception = null)
57 // everything is done as late as possible
63 public function lateCollect()
65 if (null !== $this->logger) {
66 $this->data = $this->computeErrorsCount();
67 $this->data['logs'] = $this->sanitizeLogs($this->logger->getLogs());
74 * @return array An array of logs
76 public function getLogs()
78 return isset($this->data['logs']) ? $this->data['logs'] : array();
81 public function getPriorities()
83 return isset($this->data['priorities']) ? $this->data['priorities'] : array();
86 public function countErrors()
88 return isset($this->data['error_count']) ? $this->data['error_count'] : 0;
91 public function countDeprecations()
93 return isset($this->data['deprecation_count']) ? $this->data['deprecation_count'] : 0;
96 public function countScreams()
98 return isset($this->data['scream_count']) ? $this->data['scream_count'] : 0;
104 public function getName()
109 private function sanitizeLogs($logs)
111 $errorContextById = array();
112 $sanitizedLogs = array();
114 foreach ($logs as $log) {
115 $context = $this->sanitizeContext($log['context']);
117 if (isset($context['type'], $context['file'], $context['line'], $context['level'])) {
118 $errorId = md5("{$context['type']}/{$context['line']}/{$context['file']}\x00{$log['message']}", true);
119 $silenced = !($context['type'] & $context['level']);
120 if (isset($this->errorNames[$context['type']])) {
121 $context = array_merge(array('name' => $this->errorNames[$context['type']]), $context);
124 if (isset($errorContextById[$errorId])) {
125 if (isset($errorContextById[$errorId]['errorCount'])) {
126 ++$errorContextById[$errorId]['errorCount'];
128 $errorContextById[$errorId]['errorCount'] = 2;
131 if (!$silenced && isset($errorContextById[$errorId]['scream'])) {
132 unset($errorContextById[$errorId]['scream']);
133 $errorContextById[$errorId]['level'] = $context['level'];
139 $errorContextById[$errorId] = &$context;
141 $context['scream'] = true;
144 $log['context'] = &$context;
147 $log['context'] = $context;
150 $sanitizedLogs[] = $log;
153 return $sanitizedLogs;
156 private function sanitizeContext($context)
158 if (is_array($context)) {
159 foreach ($context as $key => $value) {
160 $context[$key] = $this->sanitizeContext($value);
166 if (is_resource($context)) {
167 return sprintf('Resource(%s)', get_resource_type($context));
170 if (is_object($context)) {
171 if ($context instanceof \Exception) {
172 return sprintf('Exception(%s): %s', get_class($context), $context->getMessage());
175 return sprintf('Object(%s)', get_class($context));
181 private function computeErrorsCount()
184 'error_count' => $this->logger->countErrors(),
185 'deprecation_count' => 0,
187 'priorities' => array(),
190 foreach ($this->logger->getLogs() as $log) {
191 if (isset($count['priorities'][$log['priority']])) {
192 ++$count['priorities'][$log['priority']]['count'];
194 $count['priorities'][$log['priority']] = array(
196 'name' => $log['priorityName'],
200 if (isset($log['context']['type'], $log['context']['level'])) {
201 if (E_DEPRECATED === $log['context']['type'] || E_USER_DEPRECATED === $log['context']['type']) {
202 ++$count['deprecation_count'];
203 } elseif (!($log['context']['type'] & $log['context']['level'])) {
204 ++$count['scream_count'];
209 ksort($count['priorities']);