FOSElasticaBundle/Logger/ElasticaLogger.php

165 lines
3.7 KiB
PHP
Raw Permalink Normal View History

2011-10-04 17:01:38 +02:00
<?php
2011-10-04 17:26:14 +02:00
namespace FOS\ElasticaBundle\Logger;
2011-10-04 17:01:38 +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>
*/
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
*/
protected $debug;
2011-10-04 17:01:38 +02:00
/**
* Constructor.
*
* @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
*/
public function __construct(LoggerInterface $logger = null, $debug = false)
2011-10-04 17:01:38 +02:00
{
$this->logger = $logger;
$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
* @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
*/
public function logQuery($path, $method, $data, $time, $connection = array(), $query = array())
2011-10-04 17:01:38 +02:00
{
if ($this->debug) {
$this->queries[] = array(
'path' => $path,
'method' => $method,
'data' => $data,
'executionMS' => $time,
'connection' => $connection,
'queryString' => $query,
);
}
2011-10-04 17:01:38 +02:00
if (null !== $this->logger) {
$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;
}
/**
* {@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);
}
2011-10-04 17:01:38 +02:00
}