8000 update error handler in initializeContainer · symfony/symfony@f280c4e · GitHub
[go: up one dir, main page]

Skip to content

Commit f280c4e

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

File tree

5 files changed

+83
-115
lines changed

5 files changed

+83
-115
lines changed

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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 24 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,12 @@
124124
</div>
125125
</div>
126126

127+
{% set compilerLogTotal = 0 %}
128+
{% for logs in collector.compilerLogs %}
129+
{% set compilerLogTotal = compilerLogTotal + logs|length %}
130+
{% endfor %}
127131
<div class="tab">
128-
<h3 class="tab-title">Container Compilation<span class="badge">{{ collector.compilerLogs|length|default(0) }}</span></h3>
132+
<h3 class="tab-title">Container Compilation<span class="badge">{{ compilerLogTotal|default(0) }}</span></h3>
129133

130134
<div class="tab-content">
131135
{% if collector.compilerLogs is empty %}
@@ -136,16 +140,30 @@
136140
<table class="logs">
137141
<thead>
138142
<tr>
139-
<th class="full-width">Message</th>
143+
<th class="full-width">Class</th>
144+
<th>Messages</th>
140145
</tr>
141146
</thead>
142147

143148
<tbody>
144-
{% for log in collector.compilerLogs %}
149+
{% for class, logs in collector.compilerLogs %}
145150
<tr class="">
146151
<td class="font-normal">
147-
{{ log }}
152+
{% set context_id = 'context-compiler-' ~ loop.index %}
153+
154+
<span class="metadata">
155+
<a class="btn btn-link text-small sf-toggle" data-toggle-selector="#{{ context_id }}" data-toggle-alt-content="{{ class }}">{{ class }}</a>
156+
157+
<div id="{{ context_id }}" class="context sf-toggle-content sf-toggle-hidden">
158+
<ul>
159+
{% for log in logs %}
160+
<li>{{ log.message }}</li>
161+
{% endfor %}
162+
</ul>
163+
</div>
< F438 code>164+
</span>
148165
</td>
166+
<td class="font-normal text-right">{{ logs|length }}</td>
149167
</tr>
150168
{% endfor %}
151169
</tbody>
@@ -173,7 +191,7 @@
173191

174192
<tbody>
175193
{% for log in logs %}
176-
{% if log.priorityName %}
194+
{% if log.priorityName is defined and log.priorityName %}
177195
{% set css_class = is_deprecation ? ''
178196
: log.priorityName in ['CRITICAL', 'ERROR', 'ALERT', 'EMERGENCY'] ? 'status-error'
179197
: log.priorityName == 'WARNING' ? 'status-warning'
@@ -193,7 +211,6 @@
193211
<span class="text-muted">({{ log.errorCount }} times)</span>
194212
{% endif %}
195213
</td>
196-
197214
{% endif %}
198215

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

216233
<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 }}
234+
{{ profiler_dump(log, maxDepth=1) }}
219235
</div>
220236
</span>
221237
</td>
@@ -226,44 +242,6 @@
226242
</table>
227243
{% endmacro %}
228244

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-
267245
{% macro render_log_message(category, log_index, log, is_deprecation = false) %}
268246
{% if is_deprecation %}
269247
{{ log.message }}

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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 42 additions & 49 deletions
115
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['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
}
@@ -117,41 +101,50 @@ public function getCompilerLogs()
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();
+
}
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, '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

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -516,20 +516,17 @@ 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+
);
533530
});
534531
}
535532

@@ -541,11 +538,8 @@ protected function initializeContainer()
541538
if ($this->debug) {
542539
restore_error_handler();
543540

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-
}
541+
file_put_contents($this->getCacheDir().'/'.$class.'Deprecations.log', serialize($collectedLogs));
542+
file_put_contents($this->getCacheDir().'/'.$class.'Compiler.log', null !== $container ? implode("\n", $container->getCompiler()->getLog()) : '');
549543
}
550544
}
551545
$this->dumpContainer($cache, $container, $class, $this->getContainerBaseClass());

0 commit comments

Comments
 (0)
0