2011-10-04 17:01:38 +02:00
|
|
|
<?php
|
2011-10-04 17:26:14 +02:00
|
|
|
|
2013-03-27 18:44:06 +01:00
|
|
|
namespace FOS\ElasticaBundle\Logger;
|
2011-10-04 17:01:38 +02:00
|
|
|
|
2013-05-30 18:26:16 +02:00
|
|
|
use Psr\Log\LoggerInterface;
|
2011-10-04 17:01:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Logger for the Elastica.
|
|
|
|
*
|
|
|
|
* The {@link logQuery()} method is configured as the logger callable in the
|
|
|
|
* service container.
|
|
|
|
*
|
|
|
|
* @author Gordon Franke <info@nevalon.de>
|
|
|
|
*/
|
2013-05-30 18:26:16 +02:00
|
|
|
class ElasticaLogger implements LoggerInterface
|
2011-10-04 17:01:38 +02:00
|
|
|
{
|
2014-02-25 11:05:26 +01:00
|
|
|
/**
|
|
|
|
* @var LoggerInterface
|
|
|
|
*/
|
2011-10-04 17:01:38 +02:00
|
|
|
protected $logger;
|
2014-02-25 11:05:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $queries = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2012-08-07 20:39:14 +02:00
|
|
|
protected $debug;
|
2011-10-04 17:01:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2013-03-02 12:23:44 +01:00
|
|
|
* @param LoggerInterface|null $logger The Symfony logger
|
2014-02-25 11:05:26 +01:00
|
|
|
* @param boolean $debug
|
2011-10-04 17:01:38 +02:00
|
|
|
*/
|
2012-08-07 20:39:14 +02:00
|
|
|
public function __construct(LoggerInterface $logger = null, $debug = false)
|
2011-10-04 17:01:38 +02:00
|
|
|
{
|
|
|
|
$this->logger = $logger;
|
2012-08-07 20:39:14 +02:00
|
|
|
$this->debug = $debug;
|
2011-10-04 17:01:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logs a query.
|
|
|
|
*
|
2014-02-25 11:05:26 +01:00
|
|
|
* @param string $path Path to call
|
|
|
|
* @param string $method Rest method to use (GET, POST, DELETE, PUT)
|
|
|
|
* @param array $data Arguments
|
|
|
|
* @param float $time Execution time
|
2014-03-19 01:29:27 +01:00
|
|
|
* @param array $connection Host, port, transport, and headers of the query
|
2014-02-25 11:05:26 +01:00
|
|
|
* @param array $query Arguments
|
2011-10-04 17:01:38 +02:00
|
|
|
*/
|
2014-02-05 18:21:14 +01:00
|
|
|
public function logQuery($path, $method, $data, $time, $connection = array(), $query = array())
|
2011-10-04 17:01:38 +02:00
|
|
|
{
|
2012-08-07 20:39:14 +02:00
|
|
|
if ($this->debug) {
|
|
|
|
$this->queries[] = array(
|
|
|
|
'path' => $path,
|
|
|
|
'method' => $method,
|
|
|
|
'data' => $data,
|
2013-12-09 10:40:47 +01:00
|
|
|
'executionMS' => $time,
|
2014-02-05 18:21:14 +01:00
|
|
|
'connection' => $connection,
|
|
|
|
'queryString' => $query,
|
2012-08-07 20:39:14 +02:00
|
|
|
);
|
|
|
|
}
|
2011-10-04 17:01:38 +02:00
|
|
|
|
|
|
|
if (null !== $this->logger) {
|
2011-10-04 17:46:27 +02:00
|
|
|
$message = sprintf("%s (%s) %0.2f ms", $path, $method, $time * 1000);
|
2011-10-05 12:53:03 +02:00
|
|
|
$this->logger->info($message, (array) $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;
|
|
|
|
}
|
2013-05-30 18:26:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function emergency($message, array $context = array())
|
|
|
|
{
|
|
|
|
return $this->logger->emergency($message, $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function alert($message, array $context = array())
|
|
|
|
{
|
|
|
|
return $this->logger->alert($message, $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function critical($message, array $context = array())
|
|
|
|
{
|
|
|
|
return $this->logger->critical($message, $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function error($message, array $context = array())
|
|
|
|
{
|
|
|
|
return $this->logger->error($message, $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function warning($message, array $context = array())
|
|
|
|
{
|
|
|
|
return $this->logger->warning($message, $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function notice($message, array $context = array())
|
|
|
|
{
|
|
|
|
return $this->logger->notice($message, $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function info($message, array $context = array())
|
|
|
|
{
|
|
|
|
return $this->logger->info($message, $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function debug($message, array $context = array())
|
|
|
|
{
|
|
|
|
return $this->logger->debug($message, $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function log($level, $message, array $context = array())
|
|
|
|
{
|
2013-12-11 23:52:25 +01:00
|
|
|
return $this->logger->log($level, $message, $context);
|
2013-05-30 18:26:16 +02:00
|
|
|
}
|
2011-10-04 17:01:38 +02:00
|
|
|
}
|