2010-08-28 14:23:21 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-08-28 14:23:21 +01:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-08-28 14:23:21 +01:00
|
|
|
*
|
2011-01-15 13:29:43 +00:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2010-08-28 14:23:21 +01:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\HttpKernel\DataCollector;
|
|
|
|
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2012-12-07 11:47:14 +00:00
|
|
|
use Symfony\Component\HttpKernel\Debug\ErrorHandler;
|
2011-01-15 13:29:43 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2011-05-31 06:59:30 +01:00
|
|
|
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
|
2011-01-15 13:29:43 +00:00
|
|
|
|
2010-08-28 14:23:21 +01:00
|
|
|
/**
|
|
|
|
* LogDataCollector.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2010-08-28 14:23:21 +01:00
|
|
|
*/
|
2013-09-30 16:49:53 +01:00
|
|
|
class LoggerDataCollector extends DataCollector implements LateDataCollectorInterface
|
2010-08-28 14:23:21 +01:00
|
|
|
{
|
2011-03-23 18:47:16 +00:00
|
|
|
private $logger;
|
2010-08-28 14:23:21 +01:00
|
|
|
|
|
|
|
public function __construct($logger = null)
|
|
|
|
{
|
2011-05-31 06:59:30 +01:00
|
|
|
if (null !== $logger && $logger instanceof DebugLoggerInterface) {
|
|
|
|
$this->logger = $logger;
|
2010-08-28 14:23:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-30 06:59:16 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function collect(Request $request, Response $response, \Exception $exception = null)
|
|
|
|
{
|
|
|
|
// everything is done as late as possible
|
|
|
|
}
|
|
|
|
|
2010-08-28 14:23:21 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2013-09-30 16:49:53 +01:00
|
|
|
public function lateCollect()
|
2010-08-28 14:23:21 +01:00
|
|
|
{
|
|
|
|
if (null !== $this->logger) {
|
2014-02-04 20:08:27 +00:00
|
|
|
$this->data = $this->computeErrorsCount();
|
|
|
|
$this->data['logs'] = $this->sanitizeLogs($this->logger->getLogs());
|
2010-08-28 14:23:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the called events.
|
|
|
|
*
|
|
|
|
* @return array An array of called events
|
|
|
|
*
|
2011-03-13 18:16:56 +00:00
|
|
|
* @see TraceableEventDispatcherInterface
|
2010-08-28 14:23:21 +01:00
|
|
|
*/
|
|
|
|
public function countErrors()
|
|
|
|
{
|
|
|
|
return isset($this->data['error_count']) ? $this->data['error_count'] : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the logs.
|
|
|
|
*
|
|
|
|
* @return array An array of logs
|
|
|
|
*/
|
|
|
|
public function getLogs()
|
|
|
|
{
|
|
|
|
return isset($this->data['logs']) ? $this->data['logs'] : array();
|
|
|
|
}
|
|
|
|
|
2012-12-01 11:25:00 +00:00
|
|
|
public function countDeprecations()
|
|
|
|
{
|
|
|
|
return isset($this->data['deprecation_count']) ? $this->data['deprecation_count'] : 0;
|
|
|
|
}
|
|
|
|
|
2014-02-04 20:08:27 +00:00
|
|
|
public function countScreams()
|
|
|
|
{
|
|
|
|
return isset($this->data['scream_count']) ? $this->data['scream_count'] : 0;
|
|
|
|
}
|
|
|
|
|
2010-08-28 14:23:21 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return 'logger';
|
|
|
|
}
|
2011-06-19 11:18:50 +01:00
|
|
|
|
|
|
|
private function sanitizeLogs($logs)
|
|
|
|
{
|
|
|
|
foreach ($logs as $i => $log) {
|
|
|
|
$logs[$i]['context'] = $this->sanitizeContext($log['context']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $logs;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function sanitizeContext($context)
|
|
|
|
{
|
|
|
|
if (is_array($context)) {
|
|
|
|
foreach ($context as $key => $value) {
|
|
|
|
$context[$key] = $this->sanitizeContext($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $context;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_resource($context)) {
|
2011-06-20 09:13:52 +01:00
|
|
|
return sprintf('Resource(%s)', get_resource_type($context));
|
2011-06-19 11:18:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_object($context)) {
|
|
|
|
return sprintf('Object(%s)', get_class($context));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $context;
|
|
|
|
}
|
2012-12-01 11:25:00 +00:00
|
|
|
|
2014-02-04 20:08:27 +00:00
|
|
|
private function computeErrorsCount()
|
2012-12-01 11:25:00 +00:00
|
|
|
{
|
2014-02-04 20:08:27 +00:00
|
|
|
$count = array(
|
|
|
|
'error_count' => $this->logger->countErrors(),
|
|
|
|
'deprecation_count' => 0,
|
|
|
|
'scream_count' => 0,
|
|
|
|
);
|
|
|
|
|
2012-12-01 11:25:00 +00:00
|
|
|
foreach ($this->logger->getLogs() as $log) {
|
2014-02-04 20:08:27 +00:00
|
|
|
if (isset($log['context']['type'])) {
|
|
|
|
if (ErrorHandler::TYPE_DEPRECATION === $log['context']['type']) {
|
|
|
|
++$count['deprecation_count'];
|
|
|
|
} elseif (isset($log['context']['scream'])) {
|
|
|
|
++$count['scream_count'];
|
|
|
|
}
|
2012-12-01 11:25:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $count;
|
|
|
|
}
|
2010-08-28 14:23:21 +01:00
|
|
|
}
|