comparison vendor/symfony/http-kernel/DataCollector/LoggerDataCollector.php @ 0:4c8ae668cc8c

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