diff --git a/Client.php b/Client.php index b324434..68063be 100644 --- a/Client.php +++ b/Client.php @@ -18,10 +18,14 @@ class Client extends Elastica_Client public function request($path, $method, $data = array()) { + $start = microtime(true); + $response = parent::request($path, $method, $data); + if (null !== $this->logger) { - $this->logger->logQuery($path, $method, $data); + $time = microtime(true) - $start; + $this->logger->logQuery($path, $method, $data, $time); } - return parent::request($path, $method, $data); + return $response; } } diff --git a/Logger/ElasticaLogger.php b/Logger/ElasticaLogger.php index 6b31a8f..ffb7342 100644 --- a/Logger/ElasticaLogger.php +++ b/Logger/ElasticaLogger.php @@ -15,19 +15,16 @@ use Symfony\Component\HttpKernel\Log\LoggerInterface; class ElasticaLogger { protected $logger; - protected $prefix; protected $queries; /** * Constructor. * * @param LoggerInterface $logger The Symfony logger - * @param string $prefix A prefix for messages sent to the Symfony logger */ - public function __construct(LoggerInterface $logger = null, $prefix = 'Elastica') + public function __construct(LoggerInterface $logger = null) { $this->logger = $logger; - $this->prefix = $prefix; $this->queries = array(); } @@ -41,14 +38,18 @@ class ElasticaLogger * @param string $method Rest method to use (GET, POST, DELETE, PUT) * @param array $data OPTIONAL Arguments as array */ - public function logQuery($path, $method, array $data = array()) + public function logQuery($path, $method, array $data, $time) { - $logInfo = sprintf("%s: %s (%s) \n%s", $this->prefix, $path, $method, json_encode($data)); - - $this->queries[] = $logInfo; + $this->queries[] = array( + 'path' => $path, + 'method' => $method, + 'data' => $data, + 'executionMS' => $time + ); if (null !== $this->logger) { - $this->logger->info($logInfo); + $message = sprintf("%s (%s) %0.2f ms", $path, $method, $time * 1000); + $this->logger->info($message, $data); } } diff --git a/Resources/views/Collector/elastica.html.twig b/Resources/views/Collector/elastica.html.twig index 899b901..6fe093d 100644 --- a/Resources/views/Collector/elastica.html.twig +++ b/Resources/views/Collector/elastica.html.twig @@ -35,9 +35,14 @@