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()) public function request($path, $method, $data = array())
{ {
$start = microtime(true);
$response = parent::request($path, $method, $data);
if (null !== $this->logger) { 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 class ElasticaLogger
{ {
protected $logger; protected $logger;
protected $prefix;
protected $queries; protected $queries;
/** /**
* Constructor. * Constructor.
* *
* @param LoggerInterface $logger The Symfony logger * @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->logger = $logger;
$this->prefix = $prefix;
$this->queries = array(); $this->queries = array();
} }
@ -41,14 +38,18 @@ class ElasticaLogger
* @param string $method Rest method to use (GET, POST, DELETE, PUT) * @param string $method Rest method to use (GET, POST, DELETE, PUT)
* @param array $data OPTIONAL Arguments as array * @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[] = array(
'path' => $path,
$this->queries[] = $logInfo; 'method' => $method,
'data' => $data,
'executionMS' => $time
);
if (null !== $this->logger) { 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"> <ul class="alt">
{% for query in collector.queries %} {% for query in collector.queries %}
<li class="{{ cycle(['odd', 'even'], loop.index) }}"> <li class="{{ cycle(['odd', 'even'], loop.index) }}">
<strong>Path</strong>: {{ query.path }}<br />
<strong>Method</strong>: {{ query.method }}
<div> <div>
<code>{{ query }}</code> <code>{{ query.data|json_encode }}</code>
</div> </div>
<small>
<strong>Time</strong>: {{ '%0.2f'|format(query.executionMS * 1000) }} ms
</small>
</li> </li>
{% endfor %} {% endfor %}
</ul> </ul>