comparison vendor/symfony/http-kernel/DataCollector/LoggerDataCollector.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
22 * @author Fabien Potencier <fabien@symfony.com> 22 * @author Fabien Potencier <fabien@symfony.com>
23 */ 23 */
24 class LoggerDataCollector extends DataCollector implements LateDataCollectorInterface 24 class LoggerDataCollector extends DataCollector implements LateDataCollectorInterface
25 { 25 {
26 private $logger; 26 private $logger;
27 27 private $containerPathPrefix;
28 public function __construct($logger = null) 28
29 public function __construct($logger = null, $containerPathPrefix = null)
29 { 30 {
30 if (null !== $logger && $logger instanceof DebugLoggerInterface) { 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);
34 }
35
31 $this->logger = $logger; 36 $this->logger = $logger;
32 } 37 }
38
39 $this->containerPathPrefix = $containerPathPrefix;
33 } 40 }
34 41
35 /** 42 /**
36 * {@inheritdoc} 43 * {@inheritdoc}
37 */ 44 */
41 } 48 }
42 49
43 /** 50 /**
44 * {@inheritdoc} 51 * {@inheritdoc}
45 */ 52 */
53 public function reset()
54 {
55 if ($this->logger && method_exists($this->logger, 'clear')) {
56 $this->logger->clear();
57 }
58 $this->data = array();
59 }
60
61 /**
62 * {@inheritdoc}
63 */
46 public function lateCollect() 64 public function lateCollect()
47 { 65 {
48 if (null !== $this->logger) { 66 if (null !== $this->logger) {
49 $this->data = $this->computeErrorsCount(); 67 $containerDeprecationLogs = $this->getContainerDeprecationLogs();
50 $this->data['logs'] = $this->sanitizeLogs($this->logger->getLogs()); 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);
51 } 72 }
52 } 73 }
53 74
54 /** 75 /**
55 * Gets the logs. 76 * Gets the logs.
84 public function countScreams() 105 public function countScreams()
85 { 106 {
86 return isset($this->data['scream_count']) ? $this->data['scream_count'] : 0; 107 return isset($this->data['scream_count']) ? $this->data['scream_count'] : 0;
87 } 108 }
88 109
110 public function getCompilerLogs()
111 {
112 return isset($this->data['compiler_logs']) ? $this->data['compiler_logs'] : array();
113 }
114
89 /** 115 /**
90 * {@inheritdoc} 116 * {@inheritdoc}
91 */ 117 */
92 public function getName() 118 public function getName()
93 { 119 {
94 return 'logger'; 120 return 'logger';
95 } 121 }
96 122
123 private function getContainerDeprecationLogs()
124 {
125 if (null === $this->containerPathPrefix || !file_exists($file = $this->containerPathPrefix.'Deprecations.log')) {
126 return array();
127 }
128
129 $bootTime = filemtime($file);
130 $logs = array();
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']);
139 $logs[] = $log;
140 }
141
142 return $logs;
143 }
144
145 private function getContainerCompilerLogs()
146 {
147 if (null === $this->containerPathPrefix || !file_exists($file = $this->containerPathPrefix.'Compiler.log')) {
148 return array();
149 }
150
151 $logs = array();
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));
156 }
157
158 $logs[$log[0]][] = array('message' => $log[1]);
159 }
160
161 return $logs;
162 }
163
97 private function sanitizeLogs($logs) 164 private function sanitizeLogs($logs)
98 { 165 {
99 $sanitizedLogs = array(); 166 $sanitizedLogs = array();
167 $silencedLogs = array();
100 168
101 foreach ($logs as $log) { 169 foreach ($logs as $log) {
102 if (!$this->isSilencedOrDeprecationErrorLog($log)) { 170 if (!$this->isSilencedOrDeprecationErrorLog($log)) {
103 $log['context'] = $log['context'] ? $this->cloneVar($log['context']) : $log['context'];
104 $sanitizedLogs[] = $log; 171 $sanitizedLogs[] = $log;
105 172
106 continue; 173 continue;
107 } 174 }
108 175
176 $message = $log['message'];
109 $exception = $log['context']['exception']; 177 $exception = $log['context']['exception'];
110 $errorId = md5("{$exception->getSeverity()}/{$exception->getLine()}/{$exception->getFile()}".($exception instanceof \Exception ? "\0".$exception->getMessage() : ''), true); 178
179 if ($exception instanceof SilencedErrorContext) {
180 if (isset($silencedLogs[$h = spl_object_hash($exception)])) {
181 continue;
182 }
183 $silencedLogs[$h] = true;
184
185 if (!isset($sanitizedLogs[$message])) {
186 $sanitizedLogs[$message] = $log + array(
187 'errorCount' => 0,
188 'scream' => true,
189 );
190 }
191 $sanitizedLogs[$message]['errorCount'] += $exception->count;
192
193 continue;
194 }
195
196 $errorId = md5("{$exception->getSeverity()}/{$exception->getLine()}/{$exception->getFile()}\0{$message}", true);
111 197
112 if (isset($sanitizedLogs[$errorId])) { 198 if (isset($sanitizedLogs[$errorId])) {
113 ++$sanitizedLogs[$errorId]['errorCount']; 199 ++$sanitizedLogs[$errorId]['errorCount'];
114 } else { 200 } else {
115 $log['context'] = $log['context'] ? $this->cloneVar($log['context']) : $log['context'];
116
117 $log += array( 201 $log += array(
118 'errorCount' => 1, 202 'errorCount' => 1,
119 'scream' => $exception instanceof SilencedErrorContext, 203 'scream' => false,
120 ); 204 );
121 205
122 $sanitizedLogs[$errorId] = $log; 206 $sanitizedLogs[$errorId] = $log;
123 } 207 }
124 } 208 }
143 } 227 }
144 228
145 return false; 229 return false;
146 } 230 }
147 231
148 private function computeErrorsCount() 232 private function computeErrorsCount(array $containerDeprecationLogs)
149 { 233 {
234 $silencedLogs = array();
150 $count = array( 235 $count = array(
151 'error_count' => $this->logger->countErrors(), 236 'error_count' => $this->logger->countErrors(),
152 'deprecation_count' => 0, 237 'deprecation_count' => 0,
153 'warning_count' => 0, 238 'warning_count' => 0,
154 'scream_count' => 0, 239 'scream_count' => 0,
167 if ('WARNING' === $log['priorityName']) { 252 if ('WARNING' === $log['priorityName']) {
168 ++$count['warning_count']; 253 ++$count['warning_count'];
169 } 254 }
170 255
171 if ($this->isSilencedOrDeprecationErrorLog($log)) { 256 if ($this->isSilencedOrDeprecationErrorLog($log)) {
172 if ($log['context']['exception'] instanceof SilencedErrorContext) { 257 $exception = $log['context']['exception'];
173 ++$count['scream_count']; 258 if ($exception instanceof SilencedErrorContext) {
259 if (isset($silencedLogs[$h = spl_object_hash($exception)])) {
260 continue;
261 }
262 $silencedLogs[$h] = true;
263 $count['scream_count'] += $exception->count;
174 } else { 264 } else {
175 ++$count['deprecation_count']; 265 ++$count['deprecation_count'];
176 } 266 }
177 } 267 }
178 } 268 }
179 269
270 foreach ($containerDeprecationLogs as $deprecationLog) {
271 $count['deprecation_count'] += $deprecationLog['context']['exception']->count;
272 }
273
180 ksort($count['priorities']); 274 ksort($count['priorities']);
181 275
182 return $count; 276 return $count;
183 } 277 }
184 } 278 }