add execution time; remove logger preffix; add as second parameter

This commit is contained in:
Gordon Franke 2011-10-04 17:46:27 +02:00
parent 82b82b1908
commit d17bd572da
3 changed files with 22 additions and 12 deletions

View file

@ -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;
}
}

View file

@ -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);
}
}

View file

@ -35,9 +35,14 @@
<ul class="alt">
{% for query in collector.queries %}
<li class="{{ cycle(['odd', 'even'], loop.index) }}">
<strong>Path</strong>: {{ query.path }}<br />
<strong>Method</strong>: {{ query.method }}
<div>
<code>{{ query }}</code>
<code>{{ query.data|json_encode }}</code>
</div>
<small>
<strong>Time</strong>: {{ '%0.2f'|format(query.executionMS * 1000) }} ms
</small>
</li>
{% endfor %}
</ul>