8000 Persist app bootstrapping logs for logger datacollector · symfony/symfony@ce3ef6a · GitHub
[go: up one dir, main page]

Skip to content

Commit ce3ef6a

Browse files
ScullWMnicolas-grekas
authored andcommitted
Persist app bootstrapping logs for logger datacollector
1 parent ac5cfee commit ce3ef6a

File tree

6 files changed

+136
-14
lines changed

6 files changed

+136
-14
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +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%/%kernel.container_class%</argument>
3536
</service>
3637

3738
<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: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,52 @@
124124
</div>
125125
</div>
126126

127+
{% set compilerLogTotal = 0 %}
128+
{% for logs in collector.compilerLogs %}
129+
{% set compilerLogTotal = compilerLogTotal + logs|length %}
130+
{% endfor %}
131+
<div class="tab">
132+
<h3 class="tab-title">Container Compilation<span class="badge">{{ compilerLogTotal }}</span></h3>
133+
134+
<div class="tab-content">
135+
{% if collector.compilerLogs is empty %}
136+
<div class="empty">
137+
<p>There are no compiler log messages.</p>
138+
</div>
139+
{% else %}
140+
<table class="logs">
141+
<thead>
142+
<tr>
143+
<th class="full-width">Class</th>
144+
<th>Messages</th>
145+
</tr>
146+
</thead>
147+
148+
<tbody>
149+
{% for class, logs in collector.compilerLogs %}
150+
<tr class="">
151+
<td class="font-normal">
152+
{% set context_id = 'context-compiler-' ~ loop.index %}
153+
154+
<a class="btn btn-link sf-toggle" data-toggle-selector="#{{ context_id }}" data-toggle-alt-content="{{ class }}">{{ class }}</a>
155+
156+
<div id="{{ context_id }}" class="context sf-toggle-content sf-toggle-hidden">
157+
<ul>
158+
{% for log in logs %}
159+
<li>{{ profiler_dump_log(log.message) }}</li>
160+
{% endfor %}
161+
</ul>
162+
</div>
163+
</td>
164+
<td class="font-normal text-right">{{ logs|length }}</td>
165+
</tr>
166+
{% endfor %}
167+
</tbody>
168+
</table>
169+
{% endif %}
170+
</div>
171+
</div>
172+
127173
</div>
128174
{% endif %}
129175
{% endblock %}
@@ -165,24 +211,24 @@
165211

166212
{% endif %}
167213

168-
<td class="font-normal">{{ helper.render_log_message(category, loop.index, log, is_deprecation) }}</td>
214+
<td class="font-normal">{{ helper.render_log_message(category, loop.index, log) }}</td>
169215
</tr>
170216
{% endfor %}
171217
</tbody>
172218
</table>
173219
{% endmacro %}
174220

175-
{% macro render_log_message(category, log_index, log, is_deprecation = false) %}
176-
{% if is_deprecation %}
177-
{{ log.message }}
221+
{% macro render_log_message(category, log_index, log) %}
222+
{% if log.context.exception.trace is defined %}
223+
{{ profiler_dump_log(log.message, log.context) }}
178224

179225
{% set context_id = 'context-' ~ category ~ '-' ~ log_index %}
180226

181227
<span class="metadata">
182228
<a class="btn btn-link text-small sf-toggle" data-toggle-selector="#{{ context_id }}" data-toggle-alt-content="Hide trace">Show trace</a>
183229

184230
<div id="{{ context_id }}" class="context sf-toggle-content sf-toggle-hidden">
185-
{{ profiler_dump(log.context.exception['\0Exception\0trace'], maxDepth=2) }}
231+
{{ profiler_dump(log.context.exception.trace, maxDepth=1) }}
186232
</div>
187233
</span>
188234
{% elseif log.context is defined and log.context is not empty %}
@@ -198,6 +244,6 @@
198244
</div>
199245
</span>
200246
{% else %}
201-
{{ log.message }}
247+
{{ profiler_dump_log(log.message) }}
202248
{% endif %}
203249
{% endmacro %}

src/Symfony/Bundle/WebProfilerBundle/Twig/WebProfilerExtension.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,19 @@ public function dumpData(\Twig_Environment $env, Data $data, $maxDepth = 0)
8989
return str_replace("\n</pre", '</pre', rtrim($dump));
9090
}
9191

92-
public function dumpLog(\Twig_Environment $env, $message, Data $context)
92+
public function dumpLog(\Twig_Environment $env, $message, Data $context = null)
9393
{
9494
$message = twig_escape_filter($env, $message);
95+
$message = preg_replace('/&quot;(.*?)&quot;/', '&quot;<b>$1</b>&quot;', $message);
9596

96-
if (false === strpos($message, '{')) {
97+
if (null === $context || false === strpos($message, '{')) {
9798
return '<span class="dump-inline">'.$message.'</span>';
9899
}
99100

100101
$replacements = array();
101102
foreach ($context as $k => $v) {
102103
$k = '{'.twig_escape_filter($env, $k).'}';
103-
$replacements['&quot;'.$k.'&quot;'] = $replacements[$k] = $this->dumpData($env, $v);
104+
$replacements['&quot;<b>'.$k.'</b>&quot;'] = $replacements['&quot;'.$k.'&quot;'] = $replacements[$k] = $this->dumpData($env, $v);
104105
}
105106

106107
return '<span class="dump-inline">'.strtr($message, $replacements).'</span>';

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: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@
2424
class LoggerDataCollector extends DataCollector implements LateDataCollectorInterface
2525
{
2626
private $logger;
27+
private $containerPathPrefix;
2728

28-
public function __construct($logger = null)
29+
public function __construct($logger = null, $containerPathPrefix = null)
2930
{
3031
if (null !== $logger && $logger instanceof DebugLoggerInterface) {
3132
$this->logger = $logger;
3233
}
34+
35+
$this->containerPathPrefix = $containerPathPrefix;
3336
}
3437

3538
/**
@@ -47,7 +50,11 @@ public function lateCollect()
4750
{
4851
if (null !== $this->logger) {
4952
$this->data = $this->computeErrorsCount();
50-
$this->data['logs'] = $this->sanitizeLogs($this->logger->getLogs());
53+
54+
$containerDeprecationLogs = $this->getContainerDeprecationLogs();
55+
$this->data['deprecation_count'] += count($containerDeprecationLogs);
56+
$this->data['compiler_logs'] = $this->getContainerCompilerLogs();
57+
$this->data['logs'] = $this->sanitizeLogs(array_merge($this->logger->getLogs(), $containerDeprecationLogs));
5158
$this->data = $this->cloneVar($this->data);
5259
}
5360
}
@@ -87,6 +94,11 @@ public function countScreams()
8794
return isset($this->data['scream_count']) ? $this->data['scream_count'] : 0;
8895
}
8996

97+
public function getCompilerLogs()
98+
{
99+
return isset($this->data['compiler_logs']) ? $this->data['compiler_logs'] : array();
100+
}
101+
90102
/**
91103
* {@inheritdoc}
92104
*/
@@ -95,6 +107,44 @@ public function getName()
95107
return 'logger';
96108
}
97109

110+
private function getContainerDeprecationLogs()
111+
{
112+
if (null === $this->containerPathPrefix || !file_exists($file = $this->containerPathPrefix.'Deprecations.log')) {
113+
return array();
114+
}
115+
116+
$stubs = array();
117+
$bootTime = filemtime($file);
118+
$logs = array();
119+
foreach (unserialize(file_get_contents($file)) as $log) {
120+
$log['context'] = array('exception' => new SilencedErrorContext($log['type'], $log['file'], $log['line']));
121+
$log['timestamp'] = $bootTime;
122+
$log['priority'] = 100;
123+
$log['priorityName'] = 'DEBUG';
124+
$log['channel'] = '-';
125+
$log['scream'] = false;
126+
$logs[] = $log;
127+
}
128+
129+
return $logs;
130+
}
131+
132+
private function getContainerCompilerLogs()
133+
{
134+
if (null === $this->containerPathPrefix || !file_exists($file = $this->containerPathPrefix.'Compiler.log')) {
135+
return array();
136+
}
137+
138+
$logs = array();
139+
foreach (file($file, FILE_IGNORE_NEW_LINES) as $log) {
140+
$log = explode(': ', $log, 2);
141+
142+
$logs[$log[0]][] = array('message' => $log[1]);
143+
}
144+
145+
return $logs;
146+
}
147+
98148
private function sanitizeLogs($logs)
99149
{
100150
$sanitizedLogs = array();
@@ -107,7 +157,7 @@ private function sanitizeLogs($logs)
107157
}
108158

109159
$exception = $log['context']['exception'];
110-
$errorId = md5("{$exception->getSeverity()}/{$exception->getLine()}/{$exception->getFile()}".($exception instanceof \Exception ? "\0".$exception->getMessage() : ''), true);
160+
$errorId = md5("{$exception->getSeverity()}/{$exception->getLine()}/{$exception->getFile()}\0{$log['message']}", true);
111161

112162
if (isset($sanitizedLogs[$errorId])) {
113163
++$sanitizedLogs[$errorId]['errorCount'];

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,12 +539,32 @@ protected function initializeContainer()
539539
$cache = new ConfigCache($this->getCacheDir().'/'.$class.'.php', $this->debug);
540540
$fresh = true;
541541
if (!$cache->isFresh()) {
542-
$container = $this->buildContainer();
542+
if ($this->debug) {
543+
$collectedLogs = array();
544+
$previousHandler = set_error_handler(function ($type, $message, $file, $line) use (&$collectedLogs, &$previousHandler) {
545+
if (E_USER_DEPRECATED !== $type && E_DEPRECATED !== $type) {
546+
return $previousHandler ? $previousHandler($type, $message, $file, $line) : false;
547+
}
548+
549+
$collectedLogs[] = array(
550+
'type' => $type,
551+
'message' => $message,
552+
'file' => $file,
553+
'line' => $line,
554+
);
555+
});
556+
}
557+
543558
try {
559+
$container = null;
560+
$container = $this->buildContainer();
544561
$container->compile();
545562
} finally {
546563
if ($this->debug) {
547-
file_put_contents($this->getCacheDir().'/'.$class.'Compiler.log', implode("\n", $container->getCompiler()->getLog()));
564+
restore_error_handler();
565+
566+
file_put_contents($this->getCacheDir().'/'.$class.'Deprecations.log', serial 57B2 ize($collectedLogs));
567+
file_put_contents($this->getCacheDir().'/'.$class.'Compiler.log', null !== $container ? implode("\n", $container->getCompiler()->getLog()) : '');
548568
}
549569
}
550570
$this->dumpContainer($cache, $container, $class, $this->getContainerBaseClass());

0 commit comments

Comments
 (0)
0