8000 [Debug] add a screaming mode to ErrorHandler · symfony/symfony@5cc817d · GitHub
[go: up one dir, main page]

Skip to content

Commit 5cc817d

Browse files
[Debug] add a screaming mode to ErrorHandler
1 parent 48c9985 commit 5cc817d

File tree

7 files changed

+109
-28
lines changed

7 files changed

+109
-28
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/debug.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,12 @@
3232
<argument>deprecation</argument>
3333
<argument type="service" id="logger" on-invalid="null" />
3434
</service>
35+
36+
<service id="debug.scream_logger_listener" class="%debug.errors_logger_listener.class%">
37+
<tag name="kernel.event_subscriber" />
38+
<tag name="monolog.logger" channel="scream" />
39+
<argument>scream</argument>
40+
<argument type="service" id="logger" on-invalid="null" />
41+
</service>
3542
</services>
3643
</container>

src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/logger.html.twig

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
{% import _self as logger %}
44

55
{% block toolbar %}
6-
{% if collector.counterrors or collector.countdeprecations %}
6+
{% if collector.counterrors or collector.countdeprecations or collector.countscreams %}
77
{% set icon %}
88
<img width="15" height="28" alt="Logs" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAcCAYAAABoMT8aAAAA4klEQVQ4y2P4//8/AyWYYXgYwOPp6Xnc3t7+P7EYpB6k7+zZs2ADNEjRjIwDAgKWgAywIUfz8+fPVzg7O/8AGeCATQEQnAfi/SAah/wcV1dXvAYUgORANA75ehcXl+/4DHAABRIe+ZrhbgAhTHsDiEgHBA0glA6GfSDiw5mZma+A+sphBlhVVFQ88vHx+Xfu3Ll7QP5haOjjwtuAuGHv3r3NIMNABqh8+/atsaur666vr+9XUlwSHx//AGQANxCbAnEWyGQicRMQ9wBxIQM0qjiBWAFqkB00/glhayBWHwb1AgB38EJsUtxtWwAAAABJRU5ErkJggg==" />
99
{% if collector.counterrors %}
1010
{% set status_color = "red" %}
11-
{% else %}
11+
{% elseif collector.countdeprecations %}
1212
{% set status_color = "yellow" %}
1313
{% endif %}
14-
{% set error_count = collector.counterrors + collector.countdeprecations %}
15-
<span class="sf-toolbar-status sf-toolbar-status-{{ status_color }}">{{ error_count }}</span>
14+
{% set error_count = collector.counterrors + collector.countdeprecations + collector.countscreams %}
15+
<span class="sf-toolbar-status{% if status_color is defined %}} sf-toolbar-status-{{ status_color }}{% endif %}">{{ error_count }}</span>
1616
{% endset %}
1717
{% set text %}
1818
{% if collector.counterrors %}
@@ -27,6 +27,12 @@
2727
<span class="sf-toolbar-status sf-toolbar-status-yellow">{{ collector.countdeprecations }}</span>
2828
</div>
2929
{% endif %}
30+
{% if collector.countscreams %}
31+
<div class="sf-toolbar-info-piece">
32+
<b>Silenced Errors</b>
33+
<span class="sf-toolbar-status sf-toolbar-status">{{ collector.countscreams }}</span>
34+
</div>
35+
{% endif %}
3036
{% endset %}
3137
{% include '@WebProfiler/Profiler/toolbar_item.html.twig' with { 'link': profiler_url } %}
3238
{% endif %}
@@ -36,8 +42,8 @@
3642
<span class="label">
3743
<span class="icon"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAAgCAYAAAAMq2gFAAABjElEQVRIx2MIDw+vd3R0/GFvb/+fGtjFxeVJSUmJ1f///5nv37/PAMMMzs7OVLMEhoODgy/k5+cHJCYmagAtZAJbRG1L0DEwxCYALeOgiUXbt2+/X1NT8xTEdnd3/wi0SI4mFgHBDCBeCLXoF5BtwkCEpvNAvB8JnydCTwgQR0It+g1kWxNjUQEQOyDhAiL0gNUiWWRDjEUOyMkUZsCoRaMWjVpEvEVkFkGjFmEUqgc+fvx4hVYWIReqzi9evKileaoDslnu3LkTNLQtGk3edLPIycnpL9Bge5pb1NXVdQNosDmGRcAm7F+QgKur6783b95cBQoeRGv1kII3QPOdAoZF8+fPP4PUqnx55syZVKCEI1rLh1hsAbWEZ8aMGaUoFoFcMG3atKdIjfSPISEhawICAlaQgwMDA1f6+/sfB5rzE2Sej4/PD3C7DkjoAHHVoUOHLpSVlX3w8vL6Sa34Alr6Z8WKFaCoMARZxAHEoFZ/HBD3A/FyIF4BxMvIxCC964F4G6hZDMTxQCwJAGWE8pur5kFDAAAAAElFTkSuQmCC" alt="Logger"></span>
3844
<strong>Logs</strong>
39-
{% if collector.counterrors or collector.countdeprecations %}
40-
{% set error_count = collector.counterrors + collector.countdeprecations %}
45+
{% if collector.counterrors or collector.countdeprecations or collector.countscreams %}
46+
{% set error_count = collector.counterrors + collector.countdeprecations + collector.countscreams %}
4147
<span class="count">
4248
<span>{{ error_count }}</span>
4349
</span>
@@ -74,7 +80,7 @@
7480
{% if collector.logs %}
7581
<ul class="alt">
7682
{% for log in collector.logs if priority >= 0 and log.priority >= priority or priority < 0 and log.context.type|default(0) == priority %}
77-
<li class="{{ cycle(['odd', 'even'], loop.index) }}{% if log.priority >= 400 %} error{% elseif log.priority >= 300 %} warning{% endif %}">
83+
<li class="{{ cycle(['odd', 'even'], loop.index) }}{% if log.priority >= 400 %} error{% elseif log.priority >= 300 %} warning{% endif %}{% if log.context.scream is defined %} scream{% endif %}">
7884
{{ logger.display_message(loop.index, log) }}
7985
</li>
8086
{% else %}

src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/profiler.css.twig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,9 @@ ul.alt li.warning {
275275
background-color: #ffcc00;
276276
margin-bottom: 1px;
277277
}
278+
ul.alt li.scream, ul.alt li.scream strong {
279+
color: gray;
280+
}
278281
ul.sf-call-stack li {
279282
text-size: small;
280283
padding: 0 0 0 20px;

src/Symfony/Component/Debug/ErrorHandler.php

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Debug;
1313

14+
use Psr\Log\LogLevel;
1415
use Psr\Log\LoggerInterface;
1516
use Symfony\Component\Debug\Exception\ContextErrorException;
1617
use Symfony\Component\Debug\Exception\FatalErrorException;
@@ -44,7 +45,7 @@ class ErrorHandler
4445
E_ERROR => 'Error',
4546
E_CORE_ERROR => 'Core Error',
4647
E_COMPILE_ERROR => 'Compile Error',
47-
E_PARSE => 'Parse',
48+
E_PARSE => 'Parse Error',
4849
);
4950

5051
private $level;
@@ -108,7 +109,7 @@ public function setDisplayErrors($displayErrors)
108109
* Sets a logger for the given channel.
109110
*
110111
* @param LoggerInterface $logger A logger interface
111-
* @param string $channel The channel associated with the logger (deprecation or emergency)
112+
* @param string $channel The channel associated with the logger (deprecation, emergency or scream)
112113
*/
113114
public static function setLogger(LoggerInterface $logger, $channel = 'deprecation')
114115
{
@@ -120,10 +121,6 @@ public static function setLogger(LoggerInterface $logger, $channel = 'deprecatio
120121
*/
121122
public function handle($level, $message, $file = 'unknown', $line = 0, $context = array())
122123
{
123-
if (0 === $this->level) {
124-
return false;
125-
}
126-
127124
if ($level & (E_USER_DEPRECATED | E_DEPRECATED)) {
128125
if (isset(self::$loggers['deprecation'])) {
129126
if (self::$stackedErrorLevels) {
@@ -182,6 +179,35 @@ function ($row) {
182179
}
183180
}
184181

182+
if (isset(self::$loggers['scream']) && !(error_reporting() & $level)) {
183+
if (self::$stackedErrorLevels) {
184+
self::$stackedErrors[] = func_get_args();
185+
} else {
186+
switch ($level) {
187+
case E_USER_ERROR:
188+
case E_RECOVERABLE_ERROR:
189+
$logLevel = LogLevel::ERROR;
190+
break;
191+
192+
case E_WARNING:
193+
case E_USER_WARNING:
194+
$logLevel = LogLevel::WARNING;
195+
break;
196+
197+
default:
198+
$logLevel = LogLevel::NOTICE;
199+
break;
200+
}
201+
202+
self::$loggers['scream']->log($logLevel, $message, array(
203+
'type' => $level,
204+
'file' => $file,
205+
'line' => $line,
206+
'scream' => error_reporting(),
207+
));
208+
}
209+
}
210+
185211
return false;
186212
}
187213

src/Symfony/Component/Debug/Tests/ErrorHandlerTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,28 @@ public function testHandle()
174174
$handler->handle(E_USER_DEPRECATED, 'foo', 'foo.php', 12, 'foo');
175175

176176
restore_error_handler();
177+
178+
$logger = $this->getMock('Psr\Log\LoggerInterface');
179+
180+
$that = $this;
181+
$logArgCheck = function ($level, $message, $context) use ($that) {
182+
$that->assertEquals('Undefined variable: undefVar', $message);
183+
$that->assertArrayHasKey('type', $context);
184+
$that->assertEquals($context['type'], E_NOTICE);
185+
};
186+
187+
$logger
188+
->expects($this->once())
189+
->method('log')
190+
->will($this->returnCallback($logArgCheck))
191+
;
192+
193+
$handler = ErrorHandler::register(E_NOTICE);
194+
$handler->setLogger($logger, 'scream');
195+
unset($undefVar);
196+
@$undefVar++;
197+
198+
restore_error_handler();
177199
} catch (\Exception $e) {
178200
restore_error_handler();
179201

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,8 @@ public function collect(Request $request, Response $response, \Exception $except
4646
public function lateCollect()
4747
{
4848
if (null !== $this->logger) {
49-
$this->data = array(
50-
'error_count' => $this->logger->countErrors(),
51-
'logs' => $this->sanitizeLogs($this->logger->getLogs()),
52-
'deprecation_count' => $this->computeDeprecationCount()
53-
);
49+
$this->data = $this->computeErrorsCount();
50+
$this->data['logs'] = $this->sanitizeLogs($this->logger->getLogs());
5451
}
5552
}
5653

@@ -81,6 +78,11 @@ public function countDeprecations()
8178
return isset($this->data['deprecation_count']) ? $this->data['deprecation_count'] : 0;
8279
}
8380

81+
public function countScreams()
82+
{
83+
return isset($this->data['scream_count']) ? $this->data['scream_count'] : 0;
84+
}
85+
8486
/**
8587
* {@inheritdoc}
8688
*/
@@ -119,12 +121,21 @@ private function sanitizeContext($context)
119121
return $context;
120122
}
121123

122-
private function computeDeprecationCount()
124+
private function computeErrorsCount()
123125
{
124-
$count = 0;
126+
$count = array(
127+
'error_count' => $this->logger->countErrors(),
128+
'deprecation_count' => 0,
129+
'scream_count' => 0,
130+
);
131+
125132
foreach ($this->logger->getLogs() as $log) {
126-
if (isset($log['context']['type']) && ErrorHandler::TYPE_DEPRECATION === $log['context']['type']) {
127-
$count++;
133+
if (isset($log['context']['type'])) {
134+
if (ErrorHandler::TYPE_DEPRECATION === $log['context']['type']) {
135+
++$count['deprecation_count'];
136+
} elseif (isset($log['context']['scream'])) {
137+
++$count['scream_count'];
138+
}
128139
}
129140
}
130141

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class LoggerDataCollectorTest extends \PHPUnit_Framework_TestCase
1919
/**
2020
* @dataProvider getCollectTestData
2121
*/
22-
public function testCollect($nb, $logs, $expectedLogs, $expectedDeprecationCount)
22+
public function testCollect($nb, $logs, $expectedLogs, $expectedDeprecationCount, $expectedScreamCount)
2323
{
2424
$logger = $this->getMock('Symfony\Component\HttpKernel\Log\DebugLoggerInterface');
2525
$logger->expects($this->once())->method('countErrors')->will($this->returnValue($nb));
@@ -32,6 +32,7 @@ public function testCollect($nb, $logs, $expectedLogs, $expectedDeprecationCount
3232
$this->assertSame($nb, $c->countErrors());
3333
$this->assertSame($expectedLogs ? $expectedLogs : $logs, $c->getLogs());
3434
$this->assertSame($expectedDeprecationCount, $c->countDeprecations());
35+
$this->assertSame($expectedScreamCount, $c->countScreams());
3536
}
3637

3738
public function getCollectTestData()
@@ -41,28 +42,33 @@ public function getCollectTestData()
4142
1,
4243
array(array('message' => 'foo', 'context' => array())),
4344
null,
44-
0
45+
0,
46+
0,
4547
),
4648
array(
4749
1,
4850
array(array('message' => 'foo', 'context' => array('foo' => fopen(__FILE__, 'r')))),
4951
array(array('message' => 'foo', 'context' => array('foo' => 'Resource(stream)'))),
50-
0
52+
0,
53+
0,
5154
),
5255
array(
5356
1,
5457
array(array('message' => 'foo', 'context' => array('foo' => new \stdClass()))),
5558
array(array('message' => 'foo', 'context' => array('foo' => 'Object(stdClass)'))),
56-
0
59+
0,
60+
0,
5761
),
5862
array(
5963
1,
6064
array(
6165
array('message' => 'foo', 'context' => array('type' => ErrorHandler::TYPE_DEPRECATION)),
62-
array('message' => 'foo2', 'context' => array('type' => ErrorHandler::TYPE_DEPRECATION))
66+
array('message' => 'foo2', 'context' => array('type' => ErrorHandler::TYPE_DEPRECATION)),
67+
array('message' => 'foo3', 'context' => array('type' => E_USER_WARNING, 'scream' => 0)),
6368
),
6469
null,
65-
2
70+
2,
71+
1,
6672
),
6773
);
6874
}

0 commit comments

Comments
 (0)
0