Skip to content

Commit c0d27d6

Browse files
committed
update error handler in initializeContainer
1 parent efc326f commit c0d27d6

File tree

5 files changed

+80
-114
lines changed

5 files changed

+80
-114
lines changed

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@
3232
<tag name="data_collector" template="@WebProfiler/Collector/logger.html.twig" id="logger" priority="300" />
3333
<tag name="monolog.logger" channel="profiler" />
3434
<argument type="service" id="logger" on-invalid="ignore" />
35-
<argument>%kernel.cache_dir%</argument>
36-
<argument>%kernel.container_class%</argument>
35+
<argument>%kernel.cache_dir%/%kernel.container_class%</argument>
3736
</service>
3837

3938
<service id="data_collector.time" class="Symfony\Component\HttpKernel\DataCollector\TimeDataCollector" public="false">

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

+18-44
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,30 @@
136136
<table class="logs">
137137
<thead>
138138
<tr>
139-
<th class="full-width">Message</th>
139+
<th class="full-width">Class</th>
140+
<th>Messages</th>
140141
</tr>
141142
</thead>
142143

143144
<tbody>
144-
{% for log in collector.compilerLogs %}
145+
{% for class, logs in collector.compilerLogs %}
145146
<tr class="">
146147
<td class="font-normal">
147-
{{ log }}
148+
{% set context_id = 'context-compiler-' ~ loop.index %}
149+
150+
<span class="metadata">
151+
<a class="btn btn-link text-small sf-toggle" data-toggle-selector="#{{ context_id }}" data-toggle-alt-content="{{ class }}">{{ class }}</a>
152+
153+
<div id="{{ context_id }}" class="context sf-toggle-content sf-toggle-hidden">
154+
<ul>
155+
{% for log in logs %}
156+
<li>{{ log.message }}</li>
157+
{% endfor %}
158+
</ul>
159+
</div>
160+
</span>
148161
</td>
162+
<td class="font-normal text-right">{{ logs|length }}</td>
149163
</tr>
150164
{% endfor %}
151165
</tbody>
@@ -193,7 +207,6 @@
193207
<span class="text-muted">({{ log.errorCount }} times)</span>
194208
{% endif %}
195209
</td>
196-
197210
{% endif %}
198211

199212
<td class="font-normal">{{ helper.render_log_message(category, loop.index, log, is_deprecation) }}</td>
@@ -214,8 +227,7 @@
214227
<a class="btn btn-link text-small sf-toggle" data-toggle-selector="#{{ context_id }}" data-toggle-alt-content="Hide context">Show context</a>
215228

216229
<div id="{{ context_id }}" class="context sf-toggle-content sf-toggle-hidden">
217-
<strong>File:</strong> {{ log.file }}<br />
218-
<strong>Line:</strong> {{ log.line }}
230+
{{ profiler_dump(log, maxDepth=1) }}
219231
</div>
220232
</span>
221233
</td>
@@ -226,44 +238,6 @@
226238
</table>
227239
{% endmacro %}
228240

229-
230-
{% macro render_boot_table(logs) %}
231-
{% import _self as helper %}
232-
{% set channel_is_defined = (logs|first).channel is defined %}
233-
234-
<table class="logs">
235-
<thead>
236-
<tr>
237-
<th>Time</th>
238-
<th class="full-width">Message</th>
239-
</tr>
240-
</thead>
241-
242-
<tbody>
243-
{% for log in logs %}
244-
<tr class="">
245-
<td class="font-normal text-small" nowrap>
246-
<span class="text-muted newline">{{ log.timestamp|date('H:i:s') }}</span>
247-
</td>
248-
<td class="font-normal">
249-
{{ log.message }}
250-
{% set context_id = 'context-bootlog-' ~ loop.index %}
251-
252-
<span class="metadata">
253-
<a class="btn btn-link text-small sf-toggle" data-toggle-selector="#{{ context_id }}" data-toggle-alt-content="Hide context">Show context</a>
254-
255-
<div id="{{ context_id }}" class="context sf-toggle-content sf-toggle-hidden">
256-
<strong>File:</strong> {{ log.file }}<br />
257-
<strong>Line:</strong> {{ log.line }}
258-
</div>
259-
</span>
260-
</td>
261-
</tr>
262-
{% endfor %}
263-
</tbody>
264-
</table>
265-
{% endmacro %}
266-
267241
{% macro render_log_message(category, log_index, log, is_deprecation = false) %}
268242
{% if is_deprecation %}
269243
{{ log.message }}

src/Symfony/Component/DependencyInjection/Compiler/Compiler.php

+4
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ public function addLogMessage($string)
114114
*/
115115
public function log(CompilerPassInterface $pass, $message)
116116
{
117+
if (false !== strpos($message, "\n")) {
118+
$message = str_replace("\n", "\n".get_class($pass).': ', trim($message));
119+
}
120+
117121
$this->log[] = get_class($pass).': '.$message;
118122
}
119123

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

+43-50
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\HttpFoundation\Response;
1717
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
18+
use Symfony\Component\VarDumper\Caster\LinkStub;
1819

1920
/**
2021
* LogDataCollector.
@@ -24,25 +25,15 @@
2425
class LoggerDataCollector extends DataCollector implements LateDataCollectorInterface
2526
{
2627
private $logger;
28+
private $containerPathPrefix;
2729

28-
/**
29-
* @var string
30-
*/
31-
private $cacheDir;
32-
33-
/**
34-
* @var string
35-
*/
36-
private $containerClass;
37-
38-
public function __construct($logger = null, $cacheDir = null, $containerClass = null)
30+
public function __construct($logger = null, $containerPathPrefix = null)
3931
{
4032
if (null !== $logger && $logger instanceof DebugLoggerInterface) {
4133
$this->logger = $logger;
4234
}
4335

44-
$this->cacheDir = $cacheDir;
45-
$this->containerClass = $containerClass;
36+
$this->containerPathPrefix = $containerPathPrefix;
4637
}
4738

4839
/**
@@ -61,17 +52,10 @@ public function lateCollect()
6152
if (null !== $this->logger) {
6253
$this->data = $this->computeErrorsCount();
6354

64-
if ($bootLogs = $this->getBootLogsFile()) {
65-
$bootLogs = array_map(array($this, 'hydrateBootDeprecation'), $bootLogs);
66-
$this->data['deprecation_count'] += count($bootLogs);
67-
}
68-
69-
if ($compilerLogs = $this->getCompilerLogsFile()) {
70-
$this->data['compiler_logs'] = $compilerLogs;
71-
}
72-
73-
$this->data['logs'] = $this->sanitizeLogs($this->logger->getLogs());
74-
$this->data['logs'] = array_merge($this->data['logs'], $bootLogs);
55+
$containerDeprecationLogs = $this->getContainerDeprecationLogs();
56+
$this->data['deprecation_count'] += count($containerDeprecationLogs);
57+
$this->data['container_compiler_logs'] = $this->getContainerCompilerLogs();
58+
$this->data['logs'] = array_merge($this->sanitizeLogs($this->logger->getLogs()), $containerDeprecationLogs);
7559
$this->data = $this->cloneVar($this->data);
7660
}
7761
}
@@ -113,45 +97,54 @@ public function countScreams()
11397

11498
public function getCompilerLogs()
11599
{
116-
return isset($this->data['compiler_logs']) ? $this->data['compiler_logs'] : array();
100+
return isset($this->data['container_compiler_logs']) ? $this->data['container_compiler_logs'] : array();
117101
}
118102

119103
/**
120-
* @return array
104+
* {@inheritdoc}
121105
*/
122-
private function getBootLogsFile()
106+
public function getName()
123107
{
124-
$file = current(glob($this->cacheDir.'/'.$this->containerClass.'Boot.log'));
125-
126-
return ($file) ? unserialize(file_get_contents($file)) : array();
108+
return 'logger';
127109
}
128110

129-
/**
130-
* @return array
131-
*/
132-
private function getCompilerLogsFile()
111+
private function getContainerDeprecationLogs()
133112
{
134-
$file = current(glob($this->cacheDir.'/'.$this->containerClass.'Compiler.log'));
113+
if (null === $this->containerPathPrefix || !file_exists($file = $this->containerPathPrefix.'Deprecations.log')) {
114+
return array();
115+
}
135116

136-
return ($file) ? file($file, FILE_IGNORE_NEW_LINES) : array();
137-
}
117+
$stubs = array();
118+
$bootTime = filemtime($file);
119+
$logs = array();
120+
foreach (unserialize(file_get_contents($file)) as $log) {
121+
$log += array('scream' => false, 'is_deprecation' => true, 'priorityName' => '', 'timestamp' => $bootTime);
138122

139-
/**
140-
* {@inheritdoc}
141-
*/
142-
public function getName()
143-
{
144-
return 'logger';
123+
if (!isset($stubs[$log['file']])) {
124+
$stubs[$log['file']] = new LinkStub($log['file']);
125+
}
126+
$log['file'] = $stubs[$log['file']];
127+
128+
$logs[] = $log;
129+
}
130+
131+
return $logs;
145132
}
146133

147-
/**
148-
* @param array $log
149-
*
150-
* @return array
151-
*/
152-
private function hydrateBootDeprecation(array $log)
134+
private function getContainerCompilerLogs()
153135
{
154-
return array_merge($log, array('scream' => false, 'is_deprecation' => true, 'priorityName' => ''));
136+
if (null === $this->containerPathPrefix || !file_exists($file = $this->containerPathPrefix.'Compiler.log')) {
137+
return array();
138+
}
139+
140+
$logs = array();
141+
foreach (file($file, FILE_IGNORE_NEW_LINES) as $log) {
142+
$log = explode(': ', $log, 2);
143+
144+
$logs[$log[0]][] = array('message' => $log[1]);
145+
}
146+
147+
return $logs;
155148
}
156149

157150
private function sanitizeLogs($logs)

src/Symfony/Component/HttpKernel/Kernel.php

+14-18
Original file line numberDiff line numberDiff line change
@@ -516,20 +516,19 @@ protected function initializeContainer()
516516
if (!$cache->isFresh()) {
517517
if ($this->debug) {
518518
$collectedLogs = array();
519-
$previousHandler = set_error_handler(function () use (&$collectedLogs, &$previousHandler) {
520-
list($type, $message, $file, $line) = $args = func_get_args();
521-
522-
if (E_USER_DEPRECATED === $type) {
523-
$collectedLogs[] = array(
524-
'type' => $type,
525-
'message' => $message,
526-
'file' => $file,
527-
'line' => $line,
528-
'timestamp' => time(),
529-
);
530-
return $previousHandler ? $previousHandler : false;
519+
$previousHandler = set_error_handler(function ($type, $message, $file, $line) use (&$collectedLogs, &$previousHandler) {
520+
if (E_USER_DEPRECATED !== $type && E_DEPRECATED !== $type) {
521+
return $previousHandler ? $previousHandler($type, $message, $file, $line) : false;
531522
}
532-
return $previousHandler ? call_user_func_array($previousHandler, $args) : false;
523+
524+
$collectedLogs[] = array(
525+
'type' => $type,
526+
'message' => $message,
527+
'file' => $file,
528+
'line' => $line,
529+
);
530+
531+
return;
533532
});
534533
}
535534

@@ -541,11 +540,8 @@ protected function initializeContainer()
541540
if ($this->debug) {
542541
restore_error_handler();
543542

544-
file_put_contents($this->getCacheDir().'/'.$class.'Boot.log', serialize($collectedLogs));
545-
546-
if (null !== $container) {
547-
file_put_contents($this->getCacheDir().'/'.$class.'Compiler.log', implode("\n", $container->getCompiler()->getLog()));
548-
}
543+
file_put_contents($this->getCacheDir().'/'.$class.'Deprecations.log', serialize($collectedLogs));
544+
file_put_contents($this->getCacheDir().'/'.$class.'Compiler.log', null !== $container ? implode("\n", $container->getCompiler()->getLog()) : '');
549545
}
550546
}
551547
$this->dumpContainer($cache, $container, $class, $this->getContainerBaseClass());

0 commit comments

Comments
 (0)