Skip to content

Commit a0214ae

Browse files
nicolas-grekaswouterj
authored andcommitted
[HttpKernel] Use VarDumper in the Logs&Events panels of the profiler
1 parent d1a186c commit a0214ae

File tree

2 files changed

+42
-106
lines changed

2 files changed

+42
-106
lines changed

DataCollector/LoggerDataCollector.php

Lines changed: 20 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,6 @@
2323
*/
2424
class LoggerDataCollector extends DataCollector implements LateDataCollectorInterface
2525
{
26-
private $errorNames = array(
27-
E_DEPRECATED => 'E_DEPRECATED',
28-
E_USER_DEPRECATED => 'E_USER_DEPRECATED',
29-
E_NOTICE => 'E_NOTICE',
30-
E_USER_NOTICE => 'E_USER_NOTICE',
31-
E_STRICT => 'E_STRICT',
32-
E_WARNING => 'E_WARNING',
33-
E_USER_WARNING => 'E_USER_WARNING',
34-
E_COMPILE_WARNING => 'E_COMPILE_WARNING',
35-
E_CORE_WARNING => 'E_CORE_WARNING',
36-
E_USER_ERROR => 'E_USER_ERROR',
37-
E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
38-
E_COMPILE_ERROR => 'E_COMPILE_ERROR',
39-
E_PARSE => 'E_PARSE',
40-
E_ERROR => 'E_ERROR',
41-
E_CORE_ERROR => 'E_CORE_ERROR',
42-
);
43-
4426
private $logger;
4527

4628
public function __construct($logger = null)
@@ -94,6 +76,11 @@ public function countDeprecations()
9476
return isset($this->data['deprecation_count']) ? $this->data['deprecation_count'] : 0;
9577
}
9678

79+
public function countWarnings()
80+
{
81+
return isset($this->data['warning_count']) ? $this->data['warning_count'] : 0;
82+
}
83+
9784
public function countScreams()
9885
{
9986
return isset($this->data['scream_count']) ? $this->data['scream_count'] : 0;
@@ -109,46 +96,31 @@ public function getName()
10996

11097
private function sanitizeLogs($logs)
11198
{
112-
$errorContextById = array();
11399
$sanitizedLogs = array();
114100

115101
foreach ($logs as $log) {
116102
if (!$this->isSilencedOrDeprecationErrorLog($log)) {
117-
$log['context'] = $this->sanitizeContext($log['context']);
103+
$log['context'] = $log['context'] ? $this->cloneVar($log['context']) : $log['context'];
118104
$sanitizedLogs[] = $log;
119105

120106
continue;
121107
}
122108

123109
$exception = $log['context']['exception'];
110+
$errorId = md5("{$exception->getSeverity()}/{$exception->getLine()}/{$exception->getFile()}".($exception instanceof \Exception ? "\0".$exception->getMessage() : ''), true);
124111

125-
$context = array(
126-
'type' => isset($this->errorNames[$exception->getSeverity()]) ? $this->errorNames[$exception->getSeverity()] : $exception->getSeverity(),
127-
'file' => $exception->getFile(),
128-
'line' => $exception->getLine(),
129-
'errorCount' => 0,
130-
'scream' => $exception instanceof SilencedErrorContext,
131-
);
132-
133-
if ($exception instanceof \Exception) {
134-
$context['trace'] = array_map(function ($call) {
135-
unset($call['args']);
136-
137-
return $call;
138-
}, $exception->getTrace());
139-
}
112+
if (isset($sanitizedLogs[$errorId])) {
113+
++$sanitizedLogs[$errorId]['errorCount'];
114+
} else {
115+
$log['context'] = $log['context'] ? $this->cloneVar($log['context']) : $log['context'];
140116

141-
$errorId = md5("{$context['type']}/{$context['line']}/{$context['file']}\x00{$log['message']}", true);
117+
$log += array(
118+
'errorCount' => 1,
119+
'scream' => $exception instanceof SilencedErrorContext,
120+
);
142121

143-
if (!isset($errorContextById[$errorId])) {
144-
$errorContextById[$errorId] = $context;
122+
$sanitizedLogs[$errorId] = $log;
145123
}
146-
147-
$context['errorCount'] = ++$errorContextById[$errorId]['errorCount'];
148-
149-
$log['context'] = $this->sanitizeContext($context);
150-
151-
$sanitizedLogs[$errorId] = $log;
152124
}
153125

154126
return array_values($sanitizedLogs);
@@ -173,48 +145,12 @@ private function isSilencedOrDeprecationErrorLog(array $log)
173145
return false;
174146
}
175147

176-
private function sanitizeContext($context)
177-
{
178-
if (is_array($context)) {
179-
foreach ($context as $key => $value) {
180-
$context[$key] = $this->sanitizeContext($value);
181-
}
182-
183-
return $context;
184-
}
185-
186-
if (is_resource($context)) {
187-
return sprintf('Resource(%s)', get_resource_type($context));
188-
}
189-
190-
if ($context instanceof \Exception) {
191-
$trace = array_map(function ($call) {
192-
unset($call['args']);
193-
194-
return $call;
195-
}, $context->getTrace());
196-
197-
return array(
198-
'class' => get_class($context),
199-
'message' => $context->getMessage(),
200-
'file' => $context->getFile(),
201-
'line' => $context->getLine(),
202-
'trace' => $trace,
203-
);
204-
}
205-
206-
if (is_object($context)) {
207-
return sprintf('Object(%s)', get_class($context));
208-
}
209-
210-
return $context;
211-
}
212-
213148
private function computeErrorsCount()
214149
{
215150
$count = array(
216151
'error_count' => $this->logger->countErrors(),
217152
'deprecation_count' => 0,
153+
'warning_count' => 0,
218154
'scream_count' => 0,
219155
'priorities' => array(),
220156
);
@@ -228,6 +164,9 @@ private function computeErrorsCount()
228164
'name' => $log['priorityName'],
229165
);
230166
}
167+
if ('WARNING' === $log['priorityName']) {
168+
++$count['warning_count'];
169+
}
231170

232171
if ($this->isSilencedOrDeprecationErrorLog($log)) {
233172
if ($log['context']['exception'] instanceof SilencedErrorContext) {

Tests/DataCollector/LoggerDataCollectorTest.php

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313

1414
use Symfony\Component\Debug\Exception\SilencedErrorContext;
1515
use Symfony\Component\HttpKernel\DataCollector\LoggerDataCollector;
16+
use Symfony\Component\VarDumper\Cloner\Data;
1617

1718
class LoggerDataCollectorTest extends \PHPUnit_Framework_TestCase
1819
{
20+
private static $data;
21+
1922
/**
2023
* @dataProvider getCollectTestData
2124
*/
@@ -25,19 +28,17 @@ public function testCollect($nb, $logs, $expectedLogs, $expectedDeprecationCount
2528
$logger->expects($this->once())->method('countErrors')->will($this->returnValue($nb));
2629
$logger->expects($this->exactly(2))->method('getLogs')->will($this->returnValue($logs));
2730

28-
$c = new LoggerDataCollector($logger);
31+
// disable cloning the context, to ease fixtures creation.
32+
$c = $this->getMockBuilder(LoggerDataCollector::class)
33+
->setMethods(array('cloneVar'))
34+
->setConstructorArgs(array($logger))
35+
->getMock();
36+
$c->expects($this->any())->method('cloneVar')->willReturn(self::$data);
2937
$c->lateCollect();
3038

31-
// Remove the trace from the real logs, to ease fixtures creation.
32-
$logs = array_map(function ($log) {
33-
unset($log['context']['trace'], $log['context']['exception']['trace']);
34-
35-
return $log;
36-
}, $c->getLogs());
37-
3839
$this->assertEquals('logger', $c->getName());
3940
$this->assertEquals($nb, $c->countErrors());
40-
$this->assertEquals($expectedLogs, $logs);
41+
$this->assertEquals($expectedLogs, $c->getLogs());
4142
$this->assertEquals($expectedDeprecationCount, $c->countDeprecations());
4243
$this->assertEquals($expectedScreamCount, $c->countScreams());
4344

@@ -48,6 +49,10 @@ public function testCollect($nb, $logs, $expectedLogs, $expectedDeprecationCount
4849

4950
public function getCollectTestData()
5051
{
52+
if (null === self::$data) {
53+
self::$data = new Data(array());
54+
}
55+
5156
yield 'simple log' => array(
5257
1,
5358
array(array('message' => 'foo', 'context' => array(), 'priority' => 100, 'priorityName' => 'DEBUG')),
@@ -56,18 +61,10 @@ public function getCollectTestData()
5661
0,
5762
);
5863

59-
yield 'log with a resource' => array(
60-
1,
61-
array(array('message' => 'foo', 'context' => array('foo' => fopen(__FILE__, 'r')), 'priority' => 100, 'priorityName' => 'DEBUG')),
62-
array(array('message' => 'foo', 'context' => array('foo' => 'Resource(stream)'), 'priority' => 100, 'priorityName' => 'DEBUG')),
63-
0,
64-
0,
65-
);
66-
67-
yield 'log with an object' => array(
64+
yield 'log with a context' => array(
6865
1,
69-
array(array('message' => 'foo', 'context' => array('foo' => new \stdClass()), 'priority' => 100, 'priorityName' => 'DEBUG')),
70-
array(array('message' => 'foo', 'context' => array('foo' => 'Object(stdClass)'), 'priority' => 100, 'priorityName' => 'DEBUG')),
66+
array(array('message' => 'foo', 'context' => array('foo' => 'bar'), 'priority' => 100, 'priorityName' => 'DEBUG')),
67+
array(array('message' => 'foo', 'context' => self::$data, 'priority' => 100, 'priorityName' => 'DEBUG')),
7168
0,
7269
0,
7370
);
@@ -84,9 +81,9 @@ public function getCollectTestData()
8481
array('message' => 'foo2', 'context' => array('exception' => new \ErrorException('deprecated', 0, E_USER_DEPRECATED)), 'priority' => 100, 'priorityName' => 'DEBUG'),
8582
),
8683
array(
87-
array('message' => 'foo3', 'context' => array('exception' => array('file' => __FILE__, 'line' => 82, 'class' => \ErrorException::class, 'message' => 'warning')), 'priority' => 100, 'priorityName' => 'DEBUG'),
88-
array('message' => 'foo', 'context' => array('type' => 'E_DEPRECATED', 'file' => __FILE__, 'line' => 83, 'errorCount' => 1, 'scream' => false), 'priority' => 100, 'priorityName' => 'DEBUG'),
89-
array('message' => 'foo2', 'context' => array('type' => 'E_USER_DEPRECATED', 'file' => __FILE__, 'line' => 84, 'errorCount' => 1, 'scream' => false), 'priority' => 100, 'priorityName' => 'DEBUG'),
84+
array('message' => 'foo3', 'context' => self::$data, 'priority' => 100, 'priorityName' => 'DEBUG'),
85+
array('message' => 'foo', 'context' => self::$data, 'priority' => 100, 'priorityName' => 'DEBUG', 'errorCount' => 1, 'scream' => false),
86+
array('message' => 'foo2', 'context' => self::$data, 'priority' => 100, 'priorityName' => 'DEBUG', 'errorCount' => 1, 'scream' => false),
9087
),
9188
2,
9289
0,
@@ -100,8 +97,8 @@ public function getCollectTestData()
10097
array('message' => 'foo3', 'context' => array('exception' => new SilencedErrorContext(E_USER_WARNING, __FILE__, __LINE__)), 'priority' => 100, 'priorityName' => 'DEBUG'),
10198
),
10299
array(
103-
array('message' => 'foo3', 'context' => array('exception' => array('file' => __FILE__, 'line' => 99, 'class' => \ErrorException::class, 'message' => 'warning')), 'priority' => 100, 'priorityName' => 'DEBUG'),
104-
array('message' => 'foo3', 'context' => array('type' => 'E_USER_WARNING', 'file' => __FILE__, 'line' => 100, 'errorCount' => 1, 'scream' => true), 'priority' => 100, 'priorityName' => 'DEBUG'),
100+
array('message' => 'foo3', 'context' => self::$data, 'priority' => 100, 'priorityName' => 'DEBUG'),
101+
array('message' => 'foo3', 'context' => self::$data, 'priority' => 100, 'priorityName' => 'DEBUG', 'errorCount' => 1, 'scream' => true),
105102
),
106103
0,
107104
1,

0 commit comments

Comments
 (0)