Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\HttpKernel\DataCollector;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\Debug\Exception\SilencedErrorContext;
|
Chris@0
|
15 use Symfony\Component\HttpFoundation\Request;
|
Chris@0
|
16 use Symfony\Component\HttpFoundation\Response;
|
Chris@0
|
17 use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * LogDataCollector.
|
Chris@0
|
21 *
|
Chris@0
|
22 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
23 */
|
Chris@0
|
24 class LoggerDataCollector extends DataCollector implements LateDataCollectorInterface
|
Chris@0
|
25 {
|
Chris@0
|
26 private $logger;
|
Chris@0
|
27
|
Chris@0
|
28 public function __construct($logger = null)
|
Chris@0
|
29 {
|
Chris@0
|
30 if (null !== $logger && $logger instanceof DebugLoggerInterface) {
|
Chris@0
|
31 $this->logger = $logger;
|
Chris@0
|
32 }
|
Chris@0
|
33 }
|
Chris@0
|
34
|
Chris@0
|
35 /**
|
Chris@0
|
36 * {@inheritdoc}
|
Chris@0
|
37 */
|
Chris@0
|
38 public function collect(Request $request, Response $response, \Exception $exception = null)
|
Chris@0
|
39 {
|
Chris@0
|
40 // everything is done as late as possible
|
Chris@0
|
41 }
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * {@inheritdoc}
|
Chris@0
|
45 */
|
Chris@0
|
46 public function lateCollect()
|
Chris@0
|
47 {
|
Chris@0
|
48 if (null !== $this->logger) {
|
Chris@0
|
49 $this->data = $this->computeErrorsCount();
|
Chris@0
|
50 $this->data['logs'] = $this->sanitizeLogs($this->logger->getLogs());
|
Chris@0
|
51 }
|
Chris@0
|
52 }
|
Chris@0
|
53
|
Chris@0
|
54 /**
|
Chris@0
|
55 * Gets the logs.
|
Chris@0
|
56 *
|
Chris@0
|
57 * @return array An array of logs
|
Chris@0
|
58 */
|
Chris@0
|
59 public function getLogs()
|
Chris@0
|
60 {
|
Chris@0
|
61 return isset($this->data['logs']) ? $this->data['logs'] : array();
|
Chris@0
|
62 }
|
Chris@0
|
63
|
Chris@0
|
64 public function getPriorities()
|
Chris@0
|
65 {
|
Chris@0
|
66 return isset($this->data['priorities']) ? $this->data['priorities'] : array();
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 public function countErrors()
|
Chris@0
|
70 {
|
Chris@0
|
71 return isset($this->data['error_count']) ? $this->data['error_count'] : 0;
|
Chris@0
|
72 }
|
Chris@0
|
73
|
Chris@0
|
74 public function countDeprecations()
|
Chris@0
|
75 {
|
Chris@0
|
76 return isset($this->data['deprecation_count']) ? $this->data['deprecation_count'] : 0;
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 public function countWarnings()
|
Chris@0
|
80 {
|
Chris@0
|
81 return isset($this->data['warning_count']) ? $this->data['warning_count'] : 0;
|
Chris@0
|
82 }
|
Chris@0
|
83
|
Chris@0
|
84 public function countScreams()
|
Chris@0
|
85 {
|
Chris@0
|
86 return isset($this->data['scream_count']) ? $this->data['scream_count'] : 0;
|
Chris@0
|
87 }
|
Chris@0
|
88
|
Chris@0
|
89 /**
|
Chris@0
|
90 * {@inheritdoc}
|
Chris@0
|
91 */
|
Chris@0
|
92 public function getName()
|
Chris@0
|
93 {
|
Chris@0
|
94 return 'logger';
|
Chris@0
|
95 }
|
Chris@0
|
96
|
Chris@0
|
97 private function sanitizeLogs($logs)
|
Chris@0
|
98 {
|
Chris@0
|
99 $sanitizedLogs = array();
|
Chris@0
|
100
|
Chris@0
|
101 foreach ($logs as $log) {
|
Chris@0
|
102 if (!$this->isSilencedOrDeprecationErrorLog($log)) {
|
Chris@0
|
103 $log['context'] = $log['context'] ? $this->cloneVar($log['context']) : $log['context'];
|
Chris@0
|
104 $sanitizedLogs[] = $log;
|
Chris@0
|
105
|
Chris@0
|
106 continue;
|
Chris@0
|
107 }
|
Chris@0
|
108
|
Chris@0
|
109 $exception = $log['context']['exception'];
|
Chris@0
|
110 $errorId = md5("{$exception->getSeverity()}/{$exception->getLine()}/{$exception->getFile()}".($exception instanceof \Exception ? "\0".$exception->getMessage() : ''), true);
|
Chris@0
|
111
|
Chris@0
|
112 if (isset($sanitizedLogs[$errorId])) {
|
Chris@0
|
113 ++$sanitizedLogs[$errorId]['errorCount'];
|
Chris@0
|
114 } else {
|
Chris@0
|
115 $log['context'] = $log['context'] ? $this->cloneVar($log['context']) : $log['context'];
|
Chris@0
|
116
|
Chris@0
|
117 $log += array(
|
Chris@0
|
118 'errorCount' => 1,
|
Chris@0
|
119 'scream' => $exception instanceof SilencedErrorContext,
|
Chris@0
|
120 );
|
Chris@0
|
121
|
Chris@0
|
122 $sanitizedLogs[$errorId] = $log;
|
Chris@0
|
123 }
|
Chris@0
|
124 }
|
Chris@0
|
125
|
Chris@0
|
126 return array_values($sanitizedLogs);
|
Chris@0
|
127 }
|
Chris@0
|
128
|
Chris@0
|
129 private function isSilencedOrDeprecationErrorLog(array $log)
|
Chris@0
|
130 {
|
Chris@0
|
131 if (!isset($log['context']['exception'])) {
|
Chris@0
|
132 return false;
|
Chris@0
|
133 }
|
Chris@0
|
134
|
Chris@0
|
135 $exception = $log['context']['exception'];
|
Chris@0
|
136
|
Chris@0
|
137 if ($exception instanceof SilencedErrorContext) {
|
Chris@0
|
138 return true;
|
Chris@0
|
139 }
|
Chris@0
|
140
|
Chris@0
|
141 if ($exception instanceof \ErrorException && in_array($exception->getSeverity(), array(E_DEPRECATED, E_USER_DEPRECATED), true)) {
|
Chris@0
|
142 return true;
|
Chris@0
|
143 }
|
Chris@0
|
144
|
Chris@0
|
145 return false;
|
Chris@0
|
146 }
|
Chris@0
|
147
|
Chris@0
|
148 private function computeErrorsCount()
|
Chris@0
|
149 {
|
Chris@0
|
150 $count = array(
|
Chris@0
|
151 'error_count' => $this->logger->countErrors(),
|
Chris@0
|
152 'deprecation_count' => 0,
|
Chris@0
|
153 'warning_count' => 0,
|
Chris@0
|
154 'scream_count' => 0,
|
Chris@0
|
155 'priorities' => array(),
|
Chris@0
|
156 );
|
Chris@0
|
157
|
Chris@0
|
158 foreach ($this->logger->getLogs() as $log) {
|
Chris@0
|
159 if (isset($count['priorities'][$log['priority']])) {
|
Chris@0
|
160 ++$count['priorities'][$log['priority']]['count'];
|
Chris@0
|
161 } else {
|
Chris@0
|
162 $count['priorities'][$log['priority']] = array(
|
Chris@0
|
163 'count' => 1,
|
Chris@0
|
164 'name' => $log['priorityName'],
|
Chris@0
|
165 );
|
Chris@0
|
166 }
|
Chris@0
|
167 if ('WARNING' === $log['priorityName']) {
|
Chris@0
|
168 ++$count['warning_count'];
|
Chris@0
|
169 }
|
Chris@0
|
170
|
Chris@0
|
171 if ($this->isSilencedOrDeprecationErrorLog($log)) {
|
Chris@0
|
172 if ($log['context']['exception'] instanceof SilencedErrorContext) {
|
Chris@0
|
173 ++$count['scream_count'];
|
Chris@0
|
174 } else {
|
Chris@0
|
175 ++$count['deprecation_count'];
|
Chris@0
|
176 }
|
Chris@0
|
177 }
|
Chris@0
|
178 }
|
Chris@0
|
179
|
Chris@0
|
180 ksort($count['priorities']);
|
Chris@0
|
181
|
Chris@0
|
182 return $count;
|
Chris@0
|
183 }
|
Chris@0
|
184 }
|