FOSElasticaBundle/Logger/ElasticaLogger.php

74 lines
1.7 KiB
PHP
Raw Normal View History

2011-10-04 17:01:38 +02:00
<?php
2011-10-04 17:26:14 +02:00
2011-10-04 17:01:38 +02:00
namespace FOQ\ElasticaBundle\Logger;
use Symfony\Component\HttpKernel\Log\LoggerInterface;
/**
* Logger for the Elastica.
*
* The {@link logQuery()} method is configured as the logger callable in the
* service container.
*
* @author Gordon Franke <info@nevalon.de>
*/
class ElasticaLogger
{
protected $logger;
protected $queries;
/**
* Constructor.
*
* @param LoggerInterface $logger The Symfony logger
*/
public function __construct(LoggerInterface $logger = null)
2011-10-04 17:01:38 +02:00
{
$this->logger = $logger;
$this->queries = array();
}
/**
* Logs a query.
*
2011-10-04 17:53:28 +02:00
* @param string $path Path to call
2011-10-04 17:01:38 +02:00
* @param string $method Rest method to use (GET, POST, DELETE, PUT)
2011-10-04 17:53:28 +02:00
* @param array $data arguments
* @param float $time execution time
2011-10-04 17:01:38 +02:00
*/
public function logQuery($path, $method, array $data, $time)
2011-10-04 17:01:38 +02:00
{
$this->queries[] = array(
'path' => $path,
'method' => $method,
'data' => $data,
'executionMS' => $time
);
2011-10-04 17:01:38 +02:00
if (null !== $this->logger) {
$message = sprintf("%s (%s) %0.2f ms", $path, $method, $time * 1000);
$this->logger->info($message, $data);
2011-10-04 17:01:38 +02:00
}
}
/**
* Returns the number of queries that have been logged.
*
* @return integer The number of queries logged
*/
public function getNbQueries()
{
return count($this->queries);
}
/**
* Returns a human-readable array of queries logged.
*
* @return array An array of queries
*/
public function getQueries()
{
return $this->queries;
}
}