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

Skip to content

Commit cb03b78

Browse files
committed
Persist app bootstrapping logs for logger datacollector
1 parent 549af73 commit cb03b78

File tree

4 files changed

+77
-8
lines changed

4 files changed

+77
-8
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%</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: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
{% import _self as helper %}
44

55
{% block toolbar %}
6-
{% if collector.counterrors or collector.countdeprecations or collector.countwarnings %}
6+
{% if collector.counterrors or collector.countdeprecations or collector.countwarnings or collector.countappbootstrapping %}
77
{% set icon %}
88
{% set status_color = collector.counterrors ? 'red' : 'yellow' %}
99
{{ include('@WebProfiler/Icon/logger.svg') }}
10-
<span class="sf-toolbar-value">{{ collector.counterrors ?: (collector.countdeprecations + collector.countwarnings) }}</span>
10+
<span class="sf-toolbar-value">{{ collector.counterrors ?: (collector.countdeprecations + collector.countwarnings + collector.countappbootstrapping) }}</span>
1111
{% endset %}
1212

1313
{% set text %}
@@ -25,6 +25,11 @@
2525
<b>Deprecations</b>
2626
<span class="sf-toolbar-status sf-toolbar-status-{{ collector.countdeprecations ? 'yellow' }}">{{ collector.countdeprecations|default(0) }}</span>
2727
</div>
28+
29+
<div class="sf-toolbar-info-piece">
30+
<b>App bootstrapping</b>
31+
<span class="sf-toolbar-status sf-toolbar-status-{{ collector.countappbootstrapping ? 'yellow' }}">{{ collector.countappbootstrapping|default(0) }}</span>
32+
</div>
2833
{% endset %}
2934

3035
{{ include('@WebProfiler/Profiler/toolbar_item.html.twig', { link: profiler_url, status: status_color }) }}
@@ -35,9 +40,9 @@
3540
<span class="label label-status-{{ collector.counterrors ? 'error' : collector.countdeprecations or collector.countwarnings ? 'warning' }} {{ collector.logs is empty ? 'disabled' }}">
3641
<span class="icon">{{ include('@WebProfiler/Icon/logger.svg') }}</span>
3742
<strong>Logs</strong>
38-
{% if collector.counterrors or collector.countdeprecations or collector.countwarnings %}
43+
{% if collector.counterrors or collector.countdeprecations or collector.countwarnings or collector.countappbootstrapping %}
3944
<span class="count">
40-
<span>{{ collector.counterrors ?: (collector.countdeprecations + collector.countwarnings) }}</span>
45+
<span>{{ collector.counterrors ?: (collector.countdeprecations + collector.countwarnings + collector.countappbootstrapping) }}</span>
4146
</span>
4247
{% endif %}
4348
</span>
@@ -124,6 +129,20 @@
124129
</div>
125130
</div>
126131

132+
<div class="tab">
133+
<h3 class="tab-title">App bootstrapping<span class="badge">{{ collector.countappbootstrapping|default(0) }}</span></h3>
134+
135+
<div class="tab-content">
136+
{% if collector.appBootstrappingLogs is empty %}
137+
<div class="empty">
138+
<p>There are no log messages of this level.</p>
139+
</div>
140+
{% else %}
141+
{{ helper.render_table(collector.appBootstrappingLogs, 'app_bootstrapping', false, true) }}
142+
{% endif %}
143+
</div>
144+
</div>
145+
127146
</div>
128147
{% endif %}
129148
{% endblock %}
@@ -182,7 +201,9 @@
182201
<a class="btn btn-link text-small sf-toggle" data-toggle-selector="#{{ context_id }}" data-toggle-alt-content="Hide trace">Show trace</a>
183202

184203
<div id="{{ context_id }}" class="context sf-toggle-content sf-toggle-hidden">
204+
{% if log.context.exception is defined %}
185205
{{ profiler_dump(log.context.exception['\0Exception\0trace'], maxDepth=2) }}
206+
{% endif %}
186207
</div>
187208
</span>
188209
{% elseif log.context is defined and log.context is not empty %}

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Debug\Exception\SilencedErrorContext;
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\HttpFoundation\Response;
17+
use Symfony\Component\HttpKernel\Debug\BootLogsCollector;
1718
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
1819

1920
/**
@@ -25,11 +26,18 @@ class LoggerDataCollector extends DataCollector implements LateDataCollectorInte
2526
{
2627
private $logger;
2728

28-
public function __construct($logger = null)
29+
/**
30+
* @var string
31+
*/
32+
private $cacheDir;
33+
34+
public function __construct($logger = null, $cacheDir = null)
2935
{
3036
if (null !== $logger && $logger instanceof DebugLoggerInterface) {
3137
$this->logger = $logger;
3238
}
39+
40+
$this->cacheDir = $cacheDir;
3341
}
3442

3543
/**
@@ -48,6 +56,16 @@ public function lateCollect()
4856
if (null !== $this->logger) {
4957
$this->data = $this->computeErrorsCount();
5058
$this->data['logs'] = $this->sanitizeLogs($this->logger->getLogs());
59+
60+
$bootLogsPath = BootLogsCollector::getPath($this->cacheDir);
61+
if (file_exists($bootLogsPath)) {
62+
$bootLogsCollected = unserialize(file_get_contents($bootLogsPath));
63+
64+
$this->data['app_bootstrapping_count'] = $bootLogsCollected->countLogs();
65+
$this->data['app_bootstrapping_logs'] = $bootLogsCollected->getLogs();
66+
$this->data['app_bootstrapping_date'] = $bootLogsCollected->getCollectedAt();
67+
}
68+
5169
$this->data = $this->cloneVar($this->data);
5270
}
5371
}
@@ -87,6 +105,16 @@ public function countScreams()
87105
return isset($this->data['scream_count']) ? $this->data['scream_count'] : 0;
88106
}
89107

108+
public function getAppBootstrappingLogs()
109+
{
110+
return isset($this->data['app_bootstrapping_logs']) ? $this->data['app_bootstrapping_logs'] : array();
111+
}
112+
113+
public function countAppBootstrapping()
114+
{
115+
return isset($this->data['app_bootstrapping_count']) ? $this->data['app_bootstrapping_count'] : 0;
116+
}
117+
90118
/**
91119
* {@inheritdoc}
92120
*/
@@ -151,6 +179,7 @@ private function computeErrorsCount()
151179
'warning_count' => 0,
152180
'scream_count' => 0,
153181
'priorities' => array(),
182+
'app_bootstrapping_count' => 0,
154183
);
155184

156185
foreach ($this->logger->getLogs() as $log) {

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
2828
use Symfony\Component\HttpKernel\Config\EnvParametersResource;
2929
use Symfony\Component\HttpKernel\Config\FileLocator;
30+
use Symfony\Component\HttpKernel\Debug\BootLogsCollector;
3031
use Symfony\Component\HttpKernel\DependencyInjection\MergeExtensionConfigurationPass;
3132
use Symfony\Component\HttpKernel\DependencyInjection\AddClassesToCachePass;
3233
use Symfony\Component\Config\Loader\GlobFileLoader;
@@ -514,12 +515,29 @@ protected function initializeContainer()
514515
$cache = new ConfigCache($this->getCacheDir().'/'.$class.'.php', $this->debug);
515516
$fresh = true;
516517
if (!$cache->isFresh()) {
517-
$container = $this->buildContainer();
518+
if ($this->debug) {
519+
$collectedLogs = array();
520+
$previousHandler = set_error_handler(function () use (&$collectedLogs, &$previousHandler) {
521+
$collectedLogs[] = $args = func_get_args();
522+
523+
return $previousHandler;
524+
});
525+
}
526+
518527
try {
519-
$container->compile();
528+
$container = null;
529+
$container = $this->buildContainer();
530+
$container->compile();
520531
} finally {
521532
if ($this->debug) {
522-
file_put_contents($this->getCacheDir().'/'.$class.'Compiler.log', implode("\n", $container->getCompiler()->getLog()));
533+
restore_error_handler();
534+
535+
$bootLogsCollector = new BootLogsCollector($collectedLogs);
536+
file_put_contents($bootLogsCollector->getPath($this->getCacheDir()), serialize($bootLogsCollector));
537+
538+
if (null !== $container) {
539+
file_put_contents($this->getCacheDir().'/'.$class.'Compiler.log', implode("\n", $container->getCompiler()->getLog()));
540+
}
523541
}
524542
}
525543
$this->dumpContainer($cache, $container, $class, $this->getContainerBaseClass());

0 commit comments

Comments
 (0)
0