Skip to content

Commit 9ec1d88

Browse files
[HttpKernel] Use VarDumper in the "Logs" panel of the profiler
1 parent 0e16ca9 commit 9ec1d88

File tree

4 files changed

+83
-182
lines changed

4 files changed

+83
-182
lines changed

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

+38-75
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
{% import _self as helper %}
44

55
{% block toolbar %}
6-
{% if collector.counterrors or collector.countdeprecations or collector.countscreams %}
6+
{% if collector.counterrors or collector.countdeprecations or collector.countwarnings %}
77
{% set icon %}
8-
{% set status_color = collector.counterrors ? 'red' : collector.countdeprecations ? 'yellow' : '' %}
9-
{% set error_count = collector.counterrors + collector.countdeprecations %}
8+
{% set status_color = collector.counterrors ? 'red' : 'yellow' %}
109
{{ include('@WebProfiler/Icon/logger.svg') }}
11-
<span class="sf-toolbar-value">{{ error_count }}</span>
10+
<span class="sf-toolbar-value">{{ collector.counterrors + collector.countdeprecations + collector.countwarnings }}</span>
1211
{% endset %}
1312

1413
{% set text %}
@@ -18,13 +17,13 @@
1817
</div>
1918

2019
<div class="sf-toolbar-info-piece">
21-
<b>Deprecated Calls</b>
22-
<span class="sf-toolbar-status sf-toolbar-status-{{ collector.countdeprecations ? 'yellow' }}">{{ collector.countdeprecations|default(0) }}</span>
20+
<b>Warnings</b>
21+
<span class="sf-toolbar-status sf-toolbar-status-{{ collector.countwarnings ? 'yellow' }}">{{ collector.countwarnings|default(0) }}</span>
2322
</div>
2423

2524
<div class="sf-toolbar-info-piece">
26-
<b>Silenced Errors</b>
27-
<span class="sf-toolbar-status">{{ collector.countscreams|default(0) }}</span>
25+
<b>Deprecated Calls</b>
26+
<span class="sf-toolbar-status sf-toolbar-status-{{ collector.countdeprecations ? 'yellow' }}">{{ collector.countdeprecations|default(0) }}</span>
2827
</div>
2928
{% endset %}
3029

@@ -33,12 +32,12 @@
3332
{% endblock %}
3433

3534
{% block menu %}
36-
<span class="label label-status-{{ collector.counterrors ? 'error' : collector.countdeprecations ? 'warning' }} {{ collector.logs is empty ? 'disabled' }}">
35+
<span class="label label-status-{{ collector.counterrors ? 'error' : collector.countdeprecations or collector.countwarnings ? 'warning' }} {{ collector.logs is empty ? 'disabled' }}">
3736
<span class="icon">{{ include('@WebProfiler/Icon/logger.svg') }}</span>
3837
<strong>Logs</strong>
39-
{% if collector.counterrors or collector.countdeprecations %}
38+
{% if collector.counterrors or collector.countdeprecations or collector.countwarnings %}
4039
<span class="count">
41-
<span>{{ collector.counterrors ?: collector.countdeprecations }}</span>
40+
<span>{{ collector.counterrors + collector.countdeprecations + collector.countwarnings }}</span>
4241
</span>
4342
{% endif %}
4443
</span>
@@ -55,9 +54,9 @@
5554
{# sort collected logs in groups #}
5655
{% set deprecation_logs, debug_logs, info_and_error_logs, silenced_logs = [], [], [], [] %}
5756
{% for log in collector.logs %}
58-
{% if log.context.errorCount is defined and log.context.type is defined and log.context.type in ['E_DEPRECATED', 'E_USER_DEPRECATED'] %}
57+
{% if log.scream is defined and not log.scream %}
5958
{% set deprecation_logs = deprecation_logs|merge([log]) %}
60-
{% elseif log.context.scream is defined and log.context.scream == true %}
59+
{% elseif log.scream is defined and log.scream %}
6160
{% set silenced_logs = silenced_logs|merge([log]) %}
6261
{% elseif log.priorityName == 'DEBUG' %}
6362
{% set debug_logs = debug_logs|merge([log]) %}
@@ -138,26 +137,32 @@
138137
<tr>
139138
<th>{{ show_level ? 'Level' : 'Time' }}</th>
140139
{% if channel_is_defined %}<th>Channel</th>{% endif %}
141-
<th>Message</th>
140+
<th class="full-width">Message</th>
142141
</tr>
143142
</thead>
144143

145144
<tbody>
146145
{% for log in logs %}
147146
{% set css_class = is_deprecation ? ''
148147
: log.priorityName in ['CRITICAL', 'ERROR', 'ALERT', 'EMERGENCY'] ? 'status-error'
149-
: log.priorityName in ['NOTICE', 'WARNING'] ? 'status-warning'
148+
: log.priorityName == 'WARNING' ? 'status-warning'
150149
%}
151150
<tr class="{{ css_class }}">
152-
<td class="font-normal text-small">
151+
<td class="font-normal text-small" nowrap>
153152
{% if show_level %}
154-
<span class="colored text-bold nowrap">{{ log.priorityName }}</span>
153+
<span class="colored text-bold">{{ log.priorityName }}</span>
155154
{% endif %}
156-
<span class="text-muted nowrap newline">{{ log.timestamp|date('H:i:s') }}</span>
155+
<span class="text-muted newline">{{ log.timestamp|date('H:i:s') }}</span>
157156
</td>
158157

159158
{% if channel_is_defined %}
160-
<td class="font-normal text-small text-bold nowrap">{{ log.channel }}</td>
159+
<td class="font-normal text-small text-bold" nowrap>
160+
{{ log.channel }}
161+
{% if log.errorCount is defined and log.errorCount > 1 %}
162+
<span class="text-muted">({{ log.errorCount }} times)</span>
163+
{% endif %}
164+
</td>
165+
161166
{% endif %}
162167

163168
<td class="font-normal">{{ helper.render_log_message(category, loop.index, log, is_deprecation) }}</td>
@@ -170,67 +175,25 @@
170175
{% macro render_log_message(category, log_index, log, is_deprecation = false) %}
171176
{{ log.message }}
172177

173-
{% if log.context.errorCount is defined and log.context.errorCount > 1 %}
174-
<span class="text-small text-bold">({{ log.context.errorCount }} times)</span>
175-
{% endif %}
176-
177178
{% if is_deprecation %}
178-
{% set trace = log.context.trace|default([]) %}
179-
{% set trace_id = 'sf-call-trace-' ~ category ~ '-' ~ log_index %}
180-
181-
182-
{% if trace %}
183-
<button class="btn-link text-small sf-toggle" data-toggle-selector="#{{ trace_id }}" data-toggle-alt-content="Hide stack trace">Show stack trace</button>
184-
{% endif %}
179+
{% set context_id = 'context-' ~ category ~ '-' ~ log_index %}
185180

186-
{% for index, call in trace if index > 1 %}
187-
{% if index == 2 %}
188-
<ul class="sf-call-trace hidden" id="{{ trace_id }}">
189-
{% endif %}
181+
<span class="metadata">
182+
<a class="btn btn-link text-small sf-toggle" data-toggle-selector="#{{ context_id }}" data-toggle-alt-content="Hide trace">Show trace</a>
190183

191-
{% if call.class is defined %}
192-
{% set from = call.class|abbr_class ~ '::' ~ call.function|abbr_method() %}
193-
{% elseif call.function is defined %}
194-
{% set from = call.function|abbr_method %}
195-
{% elseif call.file is defined %}
196-
{% set from = call.file %}
197-
{% else %}
198-
{% set from = '-' %}
199-
{% endif %}
200-
201-
{% set file_name = (call.file is defined and call.line is defined) ? call.file|replace({'\\': '/'})|split('/')|last %}
202-
203-
<li>
204-
{{ from|raw }}
205-
{% if file_name %}
206-
<span class="text-small">(called from {{ call.file|format_file(call.line, file_name)|raw }})</span>
207-
{% endif %}
208-
</li>
209-
210-
{% if index == trace|length - 1 %}
211-
</ul>
212-
{% endif %}
213-
{% endfor %}
214-
{% else %}
215-
{% if log.context is defined and log.context is not empty %}
216-
{% set context_id = 'context-' ~ category ~ '-' ~ log_index %}
217-
{% set context_dump = profiler_dump(log.context) %}
218-
219-
<div class="metadata">
220-
<strong>Context</strong>:
221-
222-
{% if context_dump|length > 120 %}
223-
{{ context_dump[:120] }} ...
184+
<div id="{{ context_id }}" class="context sf-toggle-content sf-toggle-hidden">
185+
{{ profiler_dump(log.context.seek('exception').seek('\0Exception\0trace'), maxDepth=2) }}
186+
</div>
187+
</span>
188+
{% elseif log.context is defined and log.context is not empty %}
189+
{% set context_id = 'context-' ~ category ~ '-' ~ log_index %}
224190

225-
<a class="btn-link text-small sf-toggle" data-toggle-selector="#{{ context_id }}" data-toggle-alt-content="Hide full context">Show full context</a>
191+
<span class="metadata">
192+
<a class="btn btn-link text-small sf-toggle" data-toggle-selector="#{{ context_id }}" data-toggle-alt-content="Hide context">Show context</a>
226193

227-
<div id="{{ context_id }}" class="context">
228-
{{ dump(log.context) }}
229-
</div>
230-
{% else %}
231-
{{ context_dump }}
232-
{% endif %}
194+
<div id="{{ context_id }}" class="context sf-toggle-content sf-toggle-hidden">
195+
{{ profiler_dump(log.context, maxDepth=1) }}
233196
</div>
234-
{% endif %}
197+
</span>
235198
{% endif %}
236199
{% endmacro %}

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ table tbody ul {
184184
.block {
185185
display: block;
186186
}
187+
.full-width {
188+
width: 100%;
189+
}
187190
.hidden {
188191
display: none;
189192
}
@@ -823,7 +826,6 @@ table.logs .metadata {
823826
color: #777;
824827
display: block;
825828
font-size: 12px;
826-
padding-top: 4px;
827829
}
828830
table.logs .metadata strong {
829831
color: #222;

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

+20-81
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) {

0 commit comments

Comments
 (0)