8000 Fixed LoggerDataCollector behaviour with exceptions · symfony/symfony@e6cc848 · GitHub
[go: up one dir, main page]

Skip to content

Commit e6cc848

Browse files
committed
Fixed LoggerDataCollector behaviour with exceptions
1 parent f8e6112 commit e6cc848

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

src/Symfony/Component/HttpKernel/DataCollector/LoggerDataCollector.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ private function sanitizeLogs($logs)
121121

122122
foreach ($logs as $log) {
123123
if (!$this->isSilencedOrDeprecationErrorLog($log)) {
124-
$log['context'] = $log['context'] ? $this->cloneVar($log['context']) : $log['context'];
124+
$log['context'] = $log['context'] ? $this->sanitizeContext($log['context']) : $log['context'];
125125
$sanitizedLogs[] = $log;
126126

127127
continue;
@@ -153,7 +153,7 @@ private function sanitizeLogs($logs)
153153

154154
$context['errorCount'] = ++$errorContextById[$errorId]['errorCount'];
155155

156-
$log['context'] = $log['context'] ? $this->cloneVar($log['context']) : $log['context'];
156+
$log['context'] = $this->sanitizeContext($context);
157157

158158
$sanitizedLogs[$errorId] = $log;
159159
}
@@ -180,6 +180,33 @@ private function isSilencedOrDeprecationErrorLog(array $log)
180180
return false;
181181
}
182182

183+
private function sanitizeContext($context)
184+
{
185+
if (is_array($context)) {
186+
foreach ($context as $key => $value) {
187+
$context[$key] = $this->sanitizeContext($value);
188+
}
189+
}
190+
191+
if ($context instanceof \Exception) {
192+
$trace = array_map(function ($call) {
193+
unset($call['args']);
194+
195+
return $call;
196+
}, $context->getTrace());
197+
198+
return array(
199+
'class' => get_class($context),
200+
'message' => $context->getMessage(),
201+
'file' => $context->getFile(),
202+
'line' => $context->getLine(),
203+
'trace' => $trace,
204+
);
205+
}
206+
207+
return $this->cloneVar($context);
208+
}
209+
183210
private f 8000 unction computeErrorsCount()
184211
{
185212
$count = array(

src/Symfony/Component/HttpKernel/Tests/DataCollector/LoggerDataCollectorTest.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@ public function testCollect($nb, $logs, $expectedLogs, $expectedDeprecationCount
2525
$logger->expects($this->once())->method('countErrors')->will($this->returnValue($nb));
2626
$logger->expects($this->exactly(2))->method('getLogs')->will($this->returnValue($logs));
2727

28-
$c = new LoggerDataCollector($logger);
28+
// disable cloning the context, to ease fixtures creation.
29+
$c = $this->getMockBuilder(LoggerDataCollector::class)
30+
->setMethods(array('cloneVar'))
31+
->setConstructorArgs(array($logger))
32+
->getMock();
33+
$c->expects($this->any())->method('cloneVar')->will($this->returnArgument(0));
34+
2935
$c->lateCollect();
3036

3137
// Remove the trace from the real logs, to ease fixtures creation.
@@ -58,16 +64,16 @@ public function getCollectTestData()
5864

5965
yield 'log with a resource' => array(
6066
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')),
67+
array(array('message' => 'foo', 'context' => array('foo' => $file = fopen(__FILE__, 'r')), 'priority' => 100, 'priorityName' => 'DEBUG')),
68+
array(array('message' => 'foo', 'context' => array('foo' => $file), 'priority' => 100, 'priorityName' => 'DEBUG')),
6369
0,
6470
0,
6571
);
6672

6773
yield 'log with an object' => array(
6874
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')),
75+
array(array('message' => 'foo', 'context' => array('foo' => $obj = new \stdClass()), 'priority' => 100, 'priorityName' => 'DEBUG')),
76+
array(array('message' => 'foo', 'context' => array('foo' => $obj), 'priority' => 100, 'priorityName' => 'DEBUG')),
7177
0,
7278
0,
7379
);
@@ -84,9 +90,9 @@ public function getCollectTestData()
8490
array('message' => 'foo2', 'context' => array('exception' => new \ErrorException('deprecated', 0, E_USER_DEPRECATED)), 'priority' => 100, 'priorityName' => 'DEBUG'),
8591
),
8692
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'),
93+
array('message' => 'foo3', 'context' => array('exception' => array('file' => __FILE__, 'line' => 87, 'class' => \ErrorException::class, 'message' => 'warning')), 'priority' => 100, 'priorityName' => 'DEBUG'),
94+
array('message' => 'foo', 'context' => array('type' => 'E_DEPRECATED', 'file' => __FILE__, 'line' => 88, 'errorCount' => 1, 'scream' => false), 'priority' => 100, 'priorityName' => 'DEBUG'),
95+
array('message' => 'foo2', 'context' => array('type' => 'E_USER_DEPRECATED', 'file' => __FILE__, 'line' => 89, 'errorCount' => 1, 'scream' => false), 'priority' => 100, 'priorityName' => 'DEBUG'),
9096
),
9197
2,
9298
0,
@@ -100,8 +106,8 @@ public function getCollectTestData()
100106
array('message' => 'foo3', 'context' => array('exception' => new SilencedErrorContext(E_USER_WARNING, __FILE__, __LINE__)), 'priority' => 100, 'priorityName' => 'DEBUG'),
101107
),
102108
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'),
109+
array('message' => 'foo3', 'context' => array('exception' => array('file' => __FILE__, 'line' => 104, 'class' => \ErrorException::class, 'message' => 'warning')), 'priority' => 100, 'priorityName' => 'DEBUG'),
110+
array('message' => 'foo3', 'context' => array('type' => 'E_USER_WARNING', 'file' => __FILE__, 'line' => 105, 'errorCount' => 1, 'scream' => true), 'priority' => 100, 'priorityName' => 'DEBUG'),
105111
),
106112
0,
107113
1,

0 commit comments

Comments
 (0)
0